使用字符串获取资源

时间:2013-07-24 08:28:00

标签: c#

我在Resources文件夹中有很多txt文件。其中一个是corner.txt。我可以通过以下代码片段访问此文件:

Properties.Resources.corner

我将文件名保存在字符串变量中。例如:

string fileName = "corner.txt";

我想通过以下方式访问此文件:

Properties.Resources.fileName 

这可能吗?我该如何访问?

2 个答案:

答案 0 :(得分:47)

我解决了这段代码的问题:

string st = Properties.Resources.ResourceManager.GetString(tableName);

所以,我不使用文件名,我使用txt文件的字符串。这对我很有用。

非常感谢。

答案 1 :(得分:10)

你可以这样使用Reflection

var type = typeof(Properties.Resources);
var property = type.GetProperty(fileName, BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.Public);
var value = property.GetValue(null, null);

或使用ResourceManager之类的:

value = Properties.Resources.ResourceManager.GetObject(fileName, Properties.Resources.Culture);
相关问题