我有以下项目结构
/build-out/MyApp.dll /dependencies/ResFile.xml /src/MyFile.cs
在 MyFile.cs 中,我想打开位于 /dependencies 目录中的我的 ResFile.xml 并根据需要阅读它。在 Visual Studio 中一切正常,但是当我制作一个 dll 并将其与其他应用程序(作为外部库)一起使用时,我收到错误消息,因为它找不到 < em>dependencies/ResFile.xml 文件。
那么,如何将resorce文件添加到结果MyApp.dll文件中呢?
最佳答案
StackOverflow 上有几篇关于它的文章,但是一些快速注释和代码......
- 确保在“生成操作”下的属性中将该文件标记为“嵌入式资源”。
我正在使用一些代码从 DLL 中读取 html 文件,这大致就是我将其转换为字符串的方法。我希望能为您提供总体思路。
foreach (string resource in Assembly.GetExecutingAssembly().GetManifestResourceNames()) { if (resource.EndsWith("Snippet.htm")) { Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource); byte[] buff = new byte[s.Length]; s.Read(buff, 0, buff.Length); string snippet = Encoding.UTF8.GetString(buff); } }
关于c# - 在 C# 中将资源 (XML) 添加到 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607631/