C# 使用动态文件名读取存储在资源文件 (resx) 中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1097154/
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
Read a string stored in a resource file (resx) with dynamic file name
提问by
In my C# application I need to create a .resx file of strings customized for every customer.
在我的 C# 应用程序中,我需要创建一个为每个客户定制的字符串的 .resx 文件。
What I want to do is avoid recompiling the entire project every time I have to provide my application to my customer, so I need to dynamic access to this string. So, how can I access (during the app execution) to a resx file if I kwow the file name only on the execution time?
我想要做的是避免每次必须向客户提供我的应用程序时重新编译整个项目,因此我需要动态访问此字符串。那么,如果我只在执行时知道文件名,我如何(在应用程序执行期间)访问 resx 文件?
Since now I write something similar:
从现在起我写了类似的东西:
Properties.Resources.MyString1
where Resource is my Resource.resx file. But I need something like this:
其中 Resource 是我的 Resource.resx 文件。但我需要这样的东西:
GetStringFromDynamicResourceFile("MyFile.resx", "MyString1");
Is it possible?
是否可以?
Thanks Mark
谢谢马克
采纳答案by Prashanth
Will something like this help in your case?
这样的事情对你有帮助吗?
Dictionary<string, string> resourceMap = new Dictionary<string, string>();
public static void Func(string fileName)
{
ResXResourceReader rsxr = new ResXResourceReader(fileName);
foreach (DictionaryEntry d in rsxr)
{
resourceMap.Add(d.Key.ToString(),d.Value.ToString());
}
rsxr.Close();
}
public string GetResource(string resourceId)
{
return resourceMap[resourceId];
}
回答by arbiter
Of course it is possible. You need to read about ResouceSetclass in msdn. And if you want to load .resx files directly, you can use ResxResourceSet.
当然这是可能的。您需要阅读msdn 中的ResouceSet类。如果你想直接加载 .resx 文件,你可以使用ResxResourceSet。
回答by MusiGenesis
You could put the needed resources into a separate DLL (one for each customer), then extract the resources dynamically using Reflection:
您可以将所需的资源放入一个单独的 DLL(每个客户一个)中,然后使用反射动态提取资源:
Assembly ass = Assembly.LoadFromFile("customer1.dll");
string s = ass.GetManifestResource("string1");
I may have the syntax wrong - it's early. One potential caveat here: accessing a DLL through Reflection will lock the DLL file for a length of time, which may block you from updating or replacing the DLL on the client's machine.
我可能有语法错误 - 还早。这里有一个潜在的警告:通过反射访问 DLL 会将 DLL 文件锁定一段时间,这可能会阻止您更新或替换客户端计算机上的 DLL。
回答by Andriy Tolstoy
Use LINQ to SQLinstead of referencing System.Windows.Forms.ResXResourceReaderclass in your project.
使用LINQ to SQL而不是在项目中引用System.Windows.Forms.ResXResourceReader类。
public string GetStringFromDynamicResourceFile(string resxFileName, string resource)
{
return XDocument
.Load(resxFileName)
.Descendants()
.FirstOrDefault(_ => _.Attributes().Any(a => a.Value == resource))?
.Value;
}
And use it:
并使用它:
GetStringFromDynamicResourceFile("MyFile.resx", "MyString1");