58

In my solution a had a folder with a few files. All this files have the Build Action "Embedded Resource".

With this code I can get a file:

assembly.GetManifestResourceStream(assembly.GetName().Name + ".Folder.File.txt");

But is there any way to get all *.txt files in this folder? A list of names or a method to iterate through them all?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
San
  • 868
  • 1
  • 9
  • 18

4 Answers4

113

You could check out

assembly.GetManifestResourceNames()

which returns an array of strings of all the resources contained. You could then filter that list to find all your *.txt files stored as embedded resources.

See MSDN docs for GetManifestResourceNames for details.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • for some reason in `VB.net`, the output does not contain the folder names in which the resource is present vs C# – Clint Feb 08 '20 at 18:30
25

Try this, returns an array with all .txt files inside Folder directory.

private string[] GetAllTxt()
{
    var executingAssembly = Assembly.GetExecutingAssembly();
    string folderName = string.Format("{0}.Resources.Folder", executingAssembly.GetName().Name);
    return executingAssembly
        .GetManifestResourceNames()
        .Where(r => r.StartsWith(folderName) && r.EndsWith(".txt"))
        //.Select(r => r.Substring(folderName.Length + 1))
        .ToArray();
}

NOTE: Uncomment the //.Select(... line in order to get the filename.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
denys-vega
  • 3,522
  • 1
  • 19
  • 24
7

have a try with this. here you get all files

string[] embeddedResources = Assembly.GetAssembly(typeof(T)).GetManifestResourceNames();

T is of course your type. so you can use it generic

masterchris_99
  • 2,683
  • 7
  • 34
  • 55
-3

Just cracked this, use:

Assembly _assembly;
_assembly = Assembly.GetExecutingAssembly();
List<string> filenames = new List<string>();
filenames = _assembly.GetManifestResourceNames().ToList<string>();
List<string> txtFiles = new List<string>();
for (int i = 0; i < filenames.Count(); i++)
{
    string[] items = filenames.ToArray();
    if (items[i].ToString().EndsWith(".txt"))
    {
        txtFiles.Add(items[i].ToString());
    }
}