如何从资源管理器打开文件... [英] How to open a file from Resource Manager...
问题描述
我想知道如何从资源管理器打开一个文件(所有类型)(我已经在资源管理器中添加了文件。但是我想知道当我点击一个按钮时如何打开(EX。图片或任何可以单击)
I want to know how to open a file(all type) from resource manager(i already added files in resource manager..But i want to know how to open when I click a button(EX. Picture or anything that can be clicked)
推荐答案
您可以在类资源
中找到资源。给定您的顶级命名空间project被称为MyProject,你在命名空间MyProject.Properties
下找到默认的Resources-class,例如:
You find the resources in the classResources
. Given the top-level namespace of your project is called "MyProject", you find the default Resources-class under the namespaceMyProject.Properties
, e.g.:
Bitmap image1 = MyProject.Properties.Resources.Image1;
根据资源的类型,Resources-class的成员也有不同的类型:String,Bitmap,图标,字节[]等。
您可以通过导航到Resources类的定义或打开文件Resources.Designer来查看它。 .cs来自:解决方案资源管理器 - > YourProject - >属性 - > Resources.resx。
如果添加自定义资源 - 文件(通过添加 - >新商品 - >资源文件)他们可以拥有自定义名称,并且可以位于自定义命名空间下。
Depending on the type of resource the members of the Resources-class also have different types: String, Bitmap, Icon, byte[], etc.
You can take a look at it by navigating to the definition of the Resources-class or by opening the file "Resources.Designer.cs" from: Solution Explorer -> YourProject -> Properties -> Resources.resx.
If you add custom resource-files (via Add -> New Item -> Resources File) they can have custom names and can be located under custom namespaces.
您还可以使用反射来使用类似于此的方法访问嵌入资源:
You can also use reflection to access embedded resources using a method similar to this:
public static Stream GetResourceStream (string itemPath)
{
return Assembly.GetCallingAssembly().GetManifestResourceStream(itemPath);
}
然后你可以从流中创建一个图像:
You can then create an image from the stream:
new Bitmap(GetResourceStream(itemPath));
提示
另外,请确保在Visual Studio中将编译类型设置为Embedded Resource,以使其正常工作。
TIP
Also, make sure you set the compile type to "Embedded Resource" in Visual Studio for this to work.
这篇关于如何从资源管理器打开文件...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!