在 C# 中将资源(XML)添加到 DLL

Mic*_*l Z 4 c# dll resources visual-studio-2008 visual-studio

我遵循项目结构

    /build-out/MyApp.dll
    /dependencies/ResFile.xml
    /src/MyFile.cs

MyFile.cs我想开我的ResFile.xml是在/依赖目录,阅读它的一些需求。一切都像Visual Studio 中的魅力一样,但是当我制作一个dll并将它与另一个应用程序(作为外部库)一起使用时,我收到一个错误,因为它找不到依赖项/ResFile.xml文件。

那么,如何将资源文件添加到结果MyApp.dll文件中呢?

rya*_*234 5

StackOverflow 上有几篇关于它的文章,但有一些快速注释和代码......

  1. 确保在构建操作下的属性中将该文件标记为“嵌入式资源”。
  2. 我正在使用一些代码从 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);
            }
        }
    
    Run Code Online (Sandbox Code Playgroud)