4

I have a folder with Resources and want to get a list with all paths.

enter image description here

If I set them to an embedded resource, I can get them via

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

When follwing this answer https://stackoverflow.com/a/1935035/6229375, I shouldn't use embedded resource anymore or I'm doing something wrong?

Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77

2 Answers2

8

From the following blog post:

Files marked with build action of “Resource” are added to a special resx file called ProjectName.g.resx. This file is generated during the build, it is not part of the project. You can access content of the ‘Resource’ files by creating an instance of ResourceManager and calling GetStream(filename). Additionally, in WPF applications you can access these resources via Application.GetResourceStream() in C# and via things like in XAML.

   var resourceManager = new ResourceManager("ConsoleApp5.g", Assembly.GetExecutingAssembly());
    var resources = resourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true,
true);
    foreach (var res in resources)
    {
        System.Console.WriteLine(((DictionaryEntry)res).Key);
    }

where ((DictionaryEntry)res).Value will be Stream.

Access Denied
  • 8,723
  • 4
  • 42
  • 72
3

The question and the accepted solution are for "Resource" files, and in my cases I needed "Embedded Resource" files. For that, there is a built-in method: Assembly.GetManifestResourceNames

foreach(string resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
    Console.WriteLine(resourceName);
}
Mr. TA
  • 5,230
  • 1
  • 28
  • 35
  • These (manifest resources) only apply to files with build action "Embedded Resource". The question and accepted answer are referring to files with build action "Resource", which is completely different, and entirely separate. – Miral May 25 '22 at 03:24
  • @Miral oops my bad, I misread the question. Still I'll leave my (updated) answer up for those who stumble upon this from a search engine. – Mr. TA May 26 '22 at 13:43