2011-01-19 106 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; 
}