2011-01-19 18 views

答えて

5

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US"); 

// Gets the value of associated with key "MyKey" from the local resource file for a given culture ("~/MyPage.aspx.en.resx") or from the default one ("~/MyPage.aspx.resx") 
object keyValue = HttpContext.GetLocalResourceObject("~/MyPage.aspx", "MyKey", culture); 

あなたがあなたのページ/ユーザーコントロール上で直接移入する値が必要な場合、あなたはリソースファイルから値を取得するためにthese techniquesのいずれかを使用することができます。

+0

モジュール内/ FAQ/FAQ.asxc.resx – Raika

+0

@Raika:仮想パス( "GetLocalResourceObject"メソッドの最初のパラメータ)は次のようになります: "〜/ Module/FAQ/FAQ.ascx "を参照してください。私は、 "モジュール"フォルダがASP.NETアプリケーションのルートに置かれていると仮定します。 – volpav

0

この方法を使用して、リソースファイルから読み取ることができます。ファイルパスを設定に保存したり、定数にしてメソッドから削除することができます。また、より良い練習のために静的な方法にすることもできます。コードビハインドから

/// <summary> 
/// method for reading a value from a resource file 
/// (.resx file) 
/// </summary> 
/// <param name="file">file to read from</param> 
/// <param name="key">key to get the value for</param> 
/// <returns>a string value</returns> 
public string ReadResourceValue(string file, string key) 
{ 
    //value for our return value 
    string resourceValue = string.Empty; 
    try 
    { 
     // specify your resource file name 
     string resourceFile = file; 
     // get the path of your file 
     string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); 
     // create a resource manager for reading from 
     //the resx file 
     ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null); 
     // retrieve the value of the specified key 
     resourceValue = resourceManager.GetString(key); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
     resourceValue = string.Empty; 
    } 
    return resourceValue; 
} 
関連する問題