C#: Reference Application Resource File
If 开发者_如何学运维I have a File Default.html
in my project
How can I read it in code? I need the path
One Possible Solution
./Controls/MarkdownEditor/Templates/Default.html
works too. Also I set "Copy to output directory" to "Copy if newer"
Normally using this:
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"MarkDownEditMVVM.Controls.MarkDownWditor.Templates.Default.Html");
Unless you are using special namespaces.
You need to set the build action for that file to "Embedded Resource" in Visual Studio (right click the file and choose properties).
Then read the file in your code like this:
Assembly asm = Assembly.GetExecutingAssembly();
Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".Default.html");
Have you had a look at AppDomain.BaseDirectory Property or AppDomainSetup.ApplicationBase Property (System)
and Path.Combine Method (String, String) to get to the directory required?
精彩评论