Reputation: 2097
I want to read a file.xml in my project (I just want it packed in my .exe) using XmlReader. It works when I read the file from the application folder, but i want to read it from the resources so I can give out my executable without additional files.
I tried different approaches but got none of them to work. I use C# with WPF 3.0.
Can you help me?
Upvotes: 2
Views: 8351
Reputation: 4986
Add your xml file and mark it in the Properties as an "Embedded Resource". Load it with something like this:
Assembly a = Assembly.GetExecutingAssembly();
using (Stream stream = a.GetManifestResourceStream("LoadResourceTest.mydata.xml"))
using (XmlReader r = XmlReader.Create(stream))
{
...
}
My test program is LoadResourceText.exe thus the LoadResourceTest in this code.
Upvotes: 5
Reputation: 5325
If your loading anything as a resource you have to use WPF Pack URI's
Some examples:
"pack://application:,,,/ResourceFile.xaml"
"pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml"
"/ResourceFile.xaml"
"/ReferencedAssembly;component/ResourceFile.xaml"
Upvotes: 2