Search code examples
c#winformsresources.net-4.5embedded-resource

GetManifestResourceStream() returning null


I'm trying to embedd some text files on my application's resouces but when I try to retrieve it using GetManifestResourceStream() I get null. I've tried all those name combinations:

  • programName.Form1.foo.txt
  • programName.foo.txt
  • Form1.foo.txt
  • programName.Properties.Resources.resources.foo.txt

Some information:

The namespace is programName and the class where I'm calling this code from is Form1 and the file name resouce that I'm trying to retrieve is foo.txt.

the code:

var resourceName = string.Format("???.{0}", filename);
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

The file is available by using Properties.Resources.foo but as I get the file name from a string I can't use it.


Solution

  • Make sure that your file's build action is "Embedded Resource".
    If you file is in a folder you must include the folder's name.

    For example if I create a folder "xx" in my WindowsFormsApplication1 project and add a file "dictionary.txt" ,the name will be WindowsFormsApplication1.xx.dictionary.txt

    In any case just use this to see all your resources

    var files = Assembly.GetExecutingAssembly().GetManifestResourceNames();