Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all embedded resources in a folder

Tags:

c#

resources

wpf

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?

like image 827
San Avatar asked Nov 21 '11 06:11

San


People also ask

Which method returns the names of all resources in an assembly?

Assembly. GetManifestResourceNames Method (System.

What are embedded resources?

Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string -to-object dictionary that maps a name to an object. The object may be a string , an image, an icon, or a blob of bytes.

How do I add embedded resources to Visual Studio?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.


2 Answers

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.

like image 130
marc_s Avatar answered Sep 17 '22 18:09

marc_s


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.

like image 43
dbvega Avatar answered Sep 18 '22 18:09

dbvega