使用字符串获取资源
我在Resources文件夹中有很多txt文件。 其中一个是corner.txt。 我可以通过以下代码片段访问此文件:
Properties.Resources.corner
我将文件名保存在字符串变量中。 例如:
string fileName = "corner.txt";
我想通过以下方式访问此文件:
Properties.Resources.fileName
这可能吗? 我怎么才能访问?
我解决了这段代码的问题:
string st = Properties.Resources.ResourceManager.GetString(tableName);
所以,我不使用文件名,我使用txt文件的字符串。 这对我很有用。
非常感谢。
您可以像这样使用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);