Search code examples
c#.net-assemblyembedded-resource

Embedded resource file not present at runtime .Net 4


I have a file I need to access at runtime, I've included it in my project and set it up as embedded resource (it's actually a source file, I changed the extension to .cs.txt to get around VS trying to compile it. That shouldn't matter, but I'm mentioning it anyway just in case). When I try getting the file

var assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(resourceName);

I get a null. I've made sure I'm using the Namespace.Folder.Filename notation, but that didn't help. It appears the file is actually not there, because when I call

assembly.GetManifestResourceNames();

I get an empty string array. Any idea what could be the case?


Solution

  • So I got around this by using the VS resource manager. Now I can access the file directly like this:

    MyNamespace.Properties.Resources.MyFile
    

    I'd recommend this approach to anyone, as it seems not only much cleaner, but safer as well. Thanks Hans Passant for the advice.