views:

591

answers:

1

In Java you can read a file embedded inside a JAR-file by using the following code:

String file = "com/company/package/filename.txt";
InputStream is = ClassName.class.getClassLoader().getResourceAsStream(file);

What is the C#/.NET equivalent of the above code - that is, how do I read a file I've embedded inside a DLL?

Thanks!

+1  A: 

Once you've added the text file as a resource, and assigned a resourceName to it, then:

 Assembly assembly = Assembly.GetExecutingAssembly();
 TextReader inputStream = new StreamReader(assembly.GetManifestResourceStream(resourceName));
 string result = inputStream.ReadToEnd();

Note: this came from this posting

Ricardo Villamil
Thanks for your quick reply!Do you know if it possible to add a file to a DLL from the command line? I.e. not using Visual Studio. I'm running Mono and would like to add the resources as part of the build process.
knorv
Found the answer: use the -resource flag when running "mcs".
knorv