Dharma Dude
Dharma Dude

Reputation: 559

How to get the path of an embebbed resource?

I included an embebbed resource in my C# project; I know its name and how to refer to it, so I was wondering if there is any way to get its absolute path...is there?

Upvotes: 1

Views: 12111

Answers (3)

lowds
lowds

Reputation: 1145

Like others have said, an embedded resource is embedded within the compiled assembly and does not exist on the file system; if you're looking to have the file on the file system you should change the build action to 'None' and the Copy to Output Directory to one of the copy values.

If however you do mean to embed the resource then it can be accessed by using the GetManifestResourceStream method of the Assembly class as follows:

GetType().Assembly.GetManifestResourceStream("someresourcestringhere")

(The above code assumes you are accessing the resource from a class within the same assembly).

The embedded resource normally follows the following format and this is the string you would pass in to the GetManifestResourceStream method:

default project namespace.folder name (if any).file name

Any spaces in folder names are replaced with an underscore, any spaces in file names are preserved.

Personally I have found the easiest way to get this string is to use a decompiler tool (such as Telerik's Just Decompile) to have a look inside the assembly and get the full resource name for the file you're looking for.

Upvotes: 2

Damith
Damith

Reputation: 63105

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

Upvotes: 2

Jacek Gorgoń
Jacek Gorgoń

Reputation: 3214

Embedded resource is exactly that: embedded. It means, that it's included in the assembly and does not exist as a physical file after compilation (if that's what you're looking for).

Upvotes: 5

Related Questions