C# 以编程方式访问资源文件中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19465405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access the value from resource file programmatically
提问by Rohaan
Came across a situation that requires accessing string value stored in resource file based on the key provided from code behind.
遇到一种情况,需要根据后面代码提供的键访问存储在资源文件中的字符串值。
how can i do this?
我怎样才能做到这一点?
Note: the resource file exist in a web project in my solution which i want to access from my silverlight application..
注意:资源文件存在于我的解决方案中的一个 web 项目中,我想从我的 Silverlight 应用程序访问它。
回答by Zbigniew
You can use ResourceManagerclass:
您可以使用ResourceManager类:
ResourceManager myManager = new ResourceManager(typeof(MyResource));
string myString = myManager.GetString("StringKey");
回答by Coder
Try using:
尝试使用:
string resourceValue = HttpContext.GetGlobalResourceObject("resxFilename", "resourceKey").ToString();
回答by Thilina H
You can use as following no need to create ResourceManager
instance.Resource file already has inherited from ResourceManager
.
您可以使用如下无需创建ResourceManager
实例。资源文件已经继承自ResourceManager
.
string value = MyResource.ResourceManager.GetString("stringKey");
Note:MyResource
is shared resource file Name.
注:MyResource
是共享资源文件名。
回答by Monojit Sarkar
i generally access resource like this way
我通常这样访问资源
Resources.MyLocalised.CompanyName;
回答by Kshitij Jhangra
Assume that your resource file name is "Resource.resx", and you want to pass key dynamically then,
假设你的资源文件名是“Resource.resx”,然后你想动态传递key,
using System.Resources;
string resVal = Resource.ResourceManager.GetString(dynamicKeyVal);
Let me know if it not works, I will work on it and will provide the appropriate solution.
如果它不起作用,请告诉我,我会处理它并提供适当的解决方案。
回答by agileDev
If you have multiple ".resx" files of different languages under "Properties" and dynamically change the resource file. Then you get the value of a particular key from code behind like this. You don't have to mention the ".resx" file name. It will automatically get the value from the currently set ".resx" file (culture):
如果“属性”下有多个不同语言的“.resx”文件,动态更改资源文件。然后,您可以像这样从后面的代码中获取特定键的值。您不必提及“.resx”文件名。它将自动从当前设置的“.resx”文件(文化)中获取值:
string str=[ProjectName].Properties.Resources.[ResourceKeyName]