2011-01-19 114 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

我的.resx文件裏面的文件夾是這樣的:Module/FAQ/FAQ.asxc.resx – Raika 2011-01-19 12:27:25

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; 
}