Adding Resources (XML) to a DLL in C #

I follow the structure of the project

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

In MyFile.cs I want to open my ResFile.xml , which is located in the / dependencies directory and reads it for some needs. Everything works like a charm in Visual Studio, but when I create a dll and use it with other applications (like an external library), I get an error because it cannot find the dependencies / file ResFile.xml.

So, how can the resorce file be added to the MyApp.dll file?

+4
source share
1 answer

There are a few articles about this in StackOverflow, but some quick notes and code ...

  • Make sure you mark the file as “Embedded Resource” in the properties in the “Build Action” section.
  • I am using some code to read html files from a DLL, and this is about how I get it to a string. Gives you a general idea that I hope for.

    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); } } 
+4
source

Source: https://habr.com/ru/post/1387602/


All Articles