从C#中的.resx文件读取string

如何从C#中的.resx文件中读取string? 请给我指导。 一步步

此示例来自ResourceManager.GetString()上的MSDN页面 :

// Create a resource manager to retrieve resources. ResourceManager rm = new ResourceManager("items", Assembly.GetExecutingAssembly()); // Retrieve the value of the string resource named "welcome". // The resource manager will retrieve the value of the // localized resource using the caller's current culture setting. String str = rm.GetString("welcome"); 

除非从外部资源加载,否则不应该使用资源pipe理器。 对于大多数情况,假设你已经创build了一个项目(DLL,WinForms等),你只需要使用项目命名空间“Resources”和资源标识符。 例如:

假设一个项目命名空间:UberSoft.WidgetPro

而你的resx包含:

resx内容的例子

你可以使用:

 Ubersoft.WidgetPro.Properties.Resources.RESPONSE_SEARCH_WILFRED 

试试这个,为我工作..简单

假设您的资源文件名是“TestResource.resx”,并且您想要dynamic地传递密钥,

 string resVal = TestResource.ResourceManager.GetString(dynamicKeyVal); 

添加命名空间

 using System.Resources; 

打开.resx文件并将“Access Modifier”设置为Public。

 var <Variable Name> = Properties.Resources.<Resource Name> 

假设.resx文件是在项目属性下使用Visual Studio添加的,则访问该string的方式更容易,更容易出错。

  1. 在解决scheme资源pipe理器中展开.resx文件应显示一个.Designer.cs文件。
  2. 打开时,.Designer.cs文件具有一个属性名称空间和一个内部类。 对于这个例子,假设这个类被命名为Resources。
  3. 访问string就像下面这样简单:

     var resourceManager = JoshCodes.Core.Testing.Unit.Properties.Resources.ResourceManager; var exampleXmlString = resourceManager.GetString("exampleXml"); 
  4. JoshCodes.Core.Testing.Unitreplace为项目的默认名称空间。

  5. 将“exampleXml”replace为string资源的名称。

跟着@JeffH的回答,我推荐使用typeof()比string的程序集名称。

  var rm = new ResourceManager(typeof(YourAssembly.Properties.Resources)); string message = rm.GetString("NameOfKey", CultureInfo.CreateSpecificCulture("ja-JP")); 

如果由于某种原因,您不能将资源文件放在App_GlobalResources中,则可以使用ResXResourceReader或XML Reader直接打开资源文件。

以下是使用ResXResourceReader的示例代码:

  public static string GetResourceString(string ResourceName, string strKey) { //Figure out the path to where your resource files are located. //In this example, I'm figuring out the path to where a SharePoint feature directory is relative to a custom SharePoint layouts subdirectory. string currentDirectory = Path.GetDirectoryName(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"])); string featureDirectory = Path.GetFullPath(currentDirectory + "\\..\\..\\..\\FEATURES\\FEATURENAME\\Resources"); //Look for files containing the name List<string> resourceFileNameList = new List<string>(); DirectoryInfo resourceDir = new DirectoryInfo(featureDirectory); var resourceFiles = resourceDir.GetFiles(); foreach (FileInfo fi in resourceFiles) { if (fi.Name.Length > ResourceName.Length+1 && fi.Name.ToLower().Substring(0,ResourceName.Length + 1) == ResourceName.ToLower()+".") { resourceFileNameList.Add(fi.Name); } } if (resourceFileNameList.Count <= 0) { return ""; } //Get the current culture string strCulture = CultureInfo.CurrentCulture.Name; string[] cultureStrings = strCulture.Split('-'); string strLanguageString = cultureStrings[0]; string strResourceFileName=""; string strDefaultFileName = resourceFileNameList[0]; foreach (string resFileName in resourceFileNameList) { if (resFileName.ToLower() == ResourceName.ToLower() + ".resx") { strDefaultFileName = resFileName; } if (resFileName.ToLower() == ResourceName.ToLower() + "."+strCulture.ToLower() + ".resx") { strResourceFileName = resFileName; break; } else if (resFileName.ToLower() == ResourceName.ToLower() + "." + strLanguageString.ToLower() + ".resx") { strResourceFileName = resFileName; break; } } if (strResourceFileName == "") { strResourceFileName = strDefaultFileName; } //Use resx resource reader to read the file in. //https://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.aspx ResXResourceReader rsxr = new ResXResourceReader(featureDirectory + "\\"+ strResourceFileName); //IDictionaryEnumerator idenumerator = rsxr.GetEnumerator(); foreach (DictionaryEntry d in rsxr) { if (d.Key.ToString().ToLower() == strKey.ToLower()) { return d.Value.ToString(); } } return ""; } 

将一个资源(名称:ResourceName和Value:ResourceValue)添加到解决scheme/程序集后,只需使用“Properties.Resources.ResourceName”即可获取所需的资源。

我通过Visual Studio添加了.resx文件。 这创build了一个属性的designer.cs文件,立即返回任何我想要的密钥的值。 例如,这是devise器文件中的一些自动生成的代码。

 /// <summary> /// Looks up a localized string similar to When creating a Commissioning change request, you must select valid Assignees, a Type, a Component, and at least one (1) affected unit.. /// </summary> public static string MyErrorMessage { get { return ResourceManager.GetString("MyErrorMessage", resourceCulture); } } 

这样,我可以简单地做到:

 string message = Errors.MyErrorMessage; 

Errors是通过Visual Studio和MyErrorMessage创build的Errors.resx文件的关键。

我直接将我的资源文件添加到我的项目中,所以我可以使用resx文件名访问string。

例如:在Resource1.resx中,键入“resourceKey” – >“dataString”。 为了得到string“dataString”,我只是把Resource1.resourceKey。

可能有理由不这样做,我不知道,但它为我工作。

最简单的方法是:

  1. 创build一个App_GlobalResources系统文件夹,并添加一个资源文件,例如Messages.resx
  2. 在资源文件中创build您的条目,例如ErrorMsg =这是一个错误。
  3. 然后访问该条目:string errormsg = Resources.Messages.ErrorMsg