开发者

Getting file from resources by enum value

Hmmm... I have enum value (for example, 'VALUE') and some resources with txt-files. I'm getting this files ('MyResource_test.txt') using following code:

FileStream fs = new FileStream("c:\\test.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(gui.Resources.MyResource.MyResource_test);

Now I need to get file from resource by my enum value

sw.WriteLine开发者_JAVA百科(gui.Resources.MyResource.VALUE);

How to do that? Reflection or something other? Thanks


Use the ResourceManager:

sw.WriteLine(gui.Resources.MyResource.ResourceManager.GetString(YourEnumType.VALUE.ToString()));


Create a dictionary that maps the enums to the resources:

var map = new Dictionary<YourEnumType, object>();

map[YourEnumType.VALUE] = gui.Resources.MyResource.MyResource_test;

Then, when you need to write it, use the map:

sw.WriteLine(map[YourEnumType.VALUE]);

If this are standard .NET resource (a .resx file), there is an alternative. These resources are accessed through generated code which access a resource by string. If you make the names of the enum values equal to the codes of the resource items, you could access them like this:

var item = YourEnumType.VALUE;

Resource1.ResourceManager.GetString(item.ToString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜