在C#控制台应用程序中使用嵌入资源 [英] Using embedded resources in C# console application
问题描述
我试图嵌入一个XML文件到文件上通过右键单击C#控制台应用程序 - >生成操作 - >嵌入的资源
I'm trying to embed an XML file into a C# console application via Right clicking on file -> Build Action -> Embedded Resource.
我如何再上网本嵌入资源
How do I then access this embedded resource?
XDocument XMLDoc = XDocument.Load(???);
编辑:大家好,尽管收到全部扑了这个问题,这里的更新。
Hi all, despite all the bashing this question received, here's an update.
我设法得到它的工作通过使用
I managed to get it working by using
XDocument.Load(new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace.FolderName.FileName.Extension")))
这是因为没有被列入包含项目中的资源文件的文件夹名以前没有工作(没有的我发现的例子似乎有)。
It didn't work previously because the folder name containing the resource file within the project was not included (none of the examples I found seemed to have that).
谢谢大家谁试图帮助。
推荐答案
东西沿着这些路线
using System.IO;
using System.Reflection;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.XMLFile1.xml");
StreamReader reader = new StreamReader(stream);
XmlDocument doc = new XmlDocument();
doc.LoadXml(reader.ReadToEnd());
}
}
}
下面是对一个链接微软的文档,描述如何做到这一点。 http://support.microsoft.com/kb/319292
Here is a link to the Microsoft doc that describes how to do it. http://support.microsoft.com/kb/319292
这篇关于在C#控制台应用程序中使用嵌入资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!