4

I have follow project structure

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

In MyFile.cs I want to open mine ResFile.xml that is in /dependencies directory and read it for some needs. All works like a charm in Visual Studio but when I make an dll and use it with another apps(as external library) I get an error because it can't find dependencies/ResFile.xml file.

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

3
  • 2
    You'll have a bigger problem after you deploy your app on the user's machine. No project directory. A simple solution is to just copy the .xml file into the same directory as the .exe, you'll never have a problem finding it that way. Or actually embedding it as a real resource. Project + Properties, Resources tab. Commented Dec 22, 2011 at 19:22
  • @HansPassant, is there any way to include the "non-dll" dependencies of a library into the host project so that these dependencies (e.g. text file) can be changed without having to recompile the library. (If it is embedded resources we'd need to recompile the library if changing the (txt) file) Commented Feb 22, 2019 at 0:34
  • @ANewGuyInTown a potential solution here is to have a default file embedded in the dll, and use this to create an on-disk file if none exists. Then load from the on-disk file which the user can edit. If you load from the program files directory (exe location) you may need admin permissions, so could look standard app data directories instead. Commented Aug 21, 2020 at 14:18

1 Answer 1

5

There are a few articles on StackOverflow about it, but some quick notes and code ...

  1. Make sure you mark the file as an "Embedded Resource" in the properties under Build Action.
  2. I am using some code to read html files from a DLL and this is roughly how I get it into a string. Gives you the general idea I hope.

        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);
            }
        }
    
Sign up to request clarification or add additional context in comments.

1 Comment

How do you do it without making and Embedded resources? In other words, change can happen in the file without having the dll to recompile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.