如何获得一个被包围的资源的路径? [英] How to get the path of an embebbed resource?

查看:63
本文介绍了如何获得一个被包围的资源的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#项目中包含了一个隐秘的资源;我知道它的名称以及如何使用它,所以我想知道是否有任何方法可以获取它的绝对路径...在哪里?

解决方案

就像其他人所说的那样,嵌入式资源嵌入在已编译的程序集中,并且在文件系统上不存在;如果要在文件系统上保存文件,则应将构建操作更改为无",并将复制到输出目录"更改为复制值之一.

但是,如果您确实要嵌入资源,则可以使用Assembly类的GetManifestResourceStream方法访问该资源,如下所示:

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

(上面的代码假定您正在从同一程序集中的一个类访问资源).

嵌入式资源通常遵循以下格式,这是您将传递给GetManifestResourceStream方法的字符串:

默认项目名称空间.文件夹名称(如果有).文件名称

文件夹名称中的所有空格均由下划线替换,文件名称中的所有空格均会保留.

我个人发现,获取此字符串的最简单方法是使用反编译器工具(例如Telerik的Just Decompile)在程序集内部进行浏览并获取您要查找的文件的完整资源名称. /p>

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?

解决方案

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.

这篇关于如何获得一个被包围的资源的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆