This blog is moved to
http://amalhashim.wordpress.com

Thursday, June 18, 2009

C# - Reading Embedded files at Runtime

Let’s assume we have class library MyExamples.TestLibrary where we have XML-file called mappings.xml. When we compile our class library then mappings.xml will be put inside assembly as embedded resource. Here is the code to read this XML-file (you need to import System.IO, System.Reflection and System.XML namespaces).

C#
var fileName = "MyExamples.TestLibrary.mappings.xml";
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(fileName);
 
if (stream == null)
{
    throw new FileNotFoundException("Cannot find mappings file.",
fileName);
}
 
var doc = new XmlDocument();
doc.Load(stream);

No comments: