28

I have a lot of txt files in Resources folder. One of them is corner.txt. I can access this file via this code snippet:

Properties.Resources.corner

I keep file names in string variables. For example:

string fileName = "corner.txt";

I want to access this file via:

Properties.Resources.fileName 

Is this possible? How can I access?

1teamsah
  • 1,863
  • 3
  • 23
  • 43
  • It's utterly unclear what you're asking. Where is this `fileName`? Is it a generated asset? Is it a local variable you've declared in some utterly disassociated class? What? – J. Steen Jul 24 '13 at 08:30
  • 2
    Duplicate: http://stackoverflow.com/a/3314213/2524304 – Maxim Zhukov Jul 24 '13 at 08:31
  • Setting text in bold is not clearing up what you're asking for. Is fileName a full path or only the file name? Or is it a class that you want to access? – Abbas Jul 24 '13 at 08:36
  • possible duplicate of [How to read embedded resource text file](http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) – Stefan Over Jul 24 '13 at 08:41
  • http://stackoverflow.com/a/17837224/1954447 – 1teamsah Nov 05 '13 at 14:42

2 Answers2

77

I solved the problem this code snippet:

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

So, I don't use the filename, I use the txt file's string. This is useful for me.

Thanks a lot.

1teamsah
  • 1,863
  • 3
  • 23
  • 43
11

You can use Reflection like that:

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

or use the ResourceManager like that:

value = Properties.Resources.ResourceManager.GetObject(fileName, Properties.Resources.Culture);
Avram Tudor
  • 1,468
  • 12
  • 18
Swift
  • 1,861
  • 14
  • 17