How to get the path of an embebbed resource?

前端 未结 3 1247
生来不讨喜
生来不讨喜 2021-01-25 18:39

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?

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 19:07

    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.

提交回复
热议问题