This Blog Has Moved

This blog has moved to

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).

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.",
var doc = new XmlDocument();

No comments: