Friday, May 9, 2008

Get a List of Embedded Resource Names Within a .NET Application

I have a .NET Windows application and I wanted to embed an XSL file into it permanently and then reference it in code. Embedding it is just a quick setting change, but figuring out how to reference it stumped me. Here's how you can loop through all your embedded resources and see how you should reference them.
Assembly myAssemblyList = Assembly.GetExecutingAssembly();
string[] myResources = myAssemblyList.GetManifestResourceNames();
foreach (string resource in myResources)
txtMessage.Text += resource + "\r\n";
Once I had that information, here's how I could call the embedded XSL file.
Assembly embeddedXsl = Assembly.GetExecutingAssembly();
Stream myEmbeddedXslFile;
myEmbeddedXslFile =
embeddedXsl.GetManifestResourceStream("MyApp.xsl.MyXslDocument.xsl");
XmlDocument myStylesheet = new XmlDocument();
myStylesheet.Load(myEmbeddedXslFile);
It seems obvious now, but "MyApp" was the assembly name. "xsl" was the directory I had the file stored in. "MyXslDocument.xsl" was the file name.

No comments: