1

I'm using a class library in my project. The library contains an embedded resource which I want to use in my project. I tried this code in my program:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}

The problem when running this is, that the stream doesn't find anything. The problem is, that it loads the currrent assembly, which is my main project, not the class library. How can I load the resource from my library, what's the best solution in this case? Thanks a lot.

FxMx
  • 11
  • 3
  • Have you looked at this? https://stackoverflow.com/questions/1913057/how-to-get-c-net-assembly-by-name – ste-fu Dec 18 '19 at 12:02
  • Your code works fine. Did you set the text file as Embedded Resource? – VDWWD Dec 18 '19 at 12:08
  • You can try to load the assembly using `var assembly = typeof(AnyClassLibraryType).Assembly;` – Pavel Anikhouski Dec 18 '19 at 12:27
  • ok, so do I use typeof() on a random class from my class library? Because it still doesn't work... – FxMx Dec 18 '19 at 13:30
  • kk I found the error, your solution works for me (typeof). My resourceName only contained "MyFile.txt" in the program code instead of "MyCompany.MyProduct.MyFile.txt". Thank you for helping! – FxMx Dec 18 '19 at 13:44

0 Answers0