如何嵌入在.NET PE可执行文件的资源? [英] How to embed a resource in a .NET PE executable?
问题描述
我怎么能包括在.NET PE(可移植可执行)在Visual Studio 2010中的资源?
How can i include a resource in a .NET PE (Portable Executable) in Visual Studio 2010?
在<一个href="http://www.wolframalpha.com/input/?i=11/7/2011&a=%2aC.11/7/2011-_%2aDateObject.dflt-&a=DateOrder_%2a%2aMonth.Day.Year--"相对=nofollow>昔日,我们将创建一个资源脚本文件:
In the olden days we would create a resource script file:
wumpa.rc
jqueryjs RCDATA "jquery.js"
SplashLogo PNG "Hello world.png"
ReportLogo RCDATA "ReportLogo.png"
Users ICON "User XP.ico"
Toolbar BITMAP "StandardToolbar24_32bpp.bmp"
添加该文件到该项目,编译器会编译 .RC
文件;包括在最终的可执行映像的资源。
Add that file to the project, and the compiler would compile the .rc
file; including resources in the final executable image.
什么是管理/ .NET / Visual Studio的机制,包括资源?
What is the managed/.NET/Visual Studio mechanism to include resources?
- 在MSDN:资源文件(Visual Studio中)
- 在MSDN: RES协议
- 在MSDN:在MCML 使用资源LI>
- SO:什么是水库:// * /。NET中 <? / LI>
- <一个href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/097794c5-6acd-4563-8237-5e385ca7e563"相对=nofollow>使用RESX创建HTML内容://协议和嵌入式资源
- 如何构建一个包含Win32的资源管理组件使用Visual Studio 2005
- MSDN: Resource Files (Visual Studio)
- MSDN: res Protocol
- MSDN: Using Resources in MCML
- SO: What is res://*/ in .net?
- creating HTML content using resx:// protocol and embedded resources
- How to build a managed assembly that contains Win32 resources using Visual Studio 2005
这些必须的标准的资源;你知道那种每个人都可以理解为资源:
These have to be standard resources; you know the kind that everyone can read as resources:
- 资源黑客将显示为资源
- PEView 将显示为资源
- 的Internet Explorer 可以读取使用的
RES
协议(如RES:// C:\富\ MyProgram.exe / PNG / SplashLogo
)
- Resource Hacker would show as resources
- PEView would show as resources
- Internet Explorer can read using the
res
protocol (e.g.res://c:\foo\MyProgram.exe/PNG/SplashLogo
)
事情我已经试过了不起作用:
Things i've tried that don't work:
-
将资源添加到
Resources.resx
文件:
将资源添加到 Resources.resx
的文件,并指定的生成操作资源
Adding resources to the Resources.resx
file, and specifying a build action of Resource:
(也尝试建立动作:嵌入的资源,作为<一个href="http://stackoverflow.com/questions/278838/visual-studio-how-to-store-an-image-resource-as-an-embedded-resource">was 2008年向我建议)
(also tried Build actions: Embedded Resource, as was suggested to me in 2008)
更新:什么没有工作
我尝试添加一个文件(<$ C C $> wumpa.rc )到项目:
i tried adding a file (wumpa.rc
) to the project:
wumpa.rc
SplashPNG PNG "Splash.png"
在默认情况下它不工作。我试图改变的生成操作的 wumpa.rc
:
- 内容(默认):没有工作
- 编译:命名空间不能直接包含的成员,如字段或方法的
- 嵌入的资源:没有工作
- 资源:没有工作
- Content (the default): didn't work
- Compile: "A namespace cannot directly contain members such as fields or methods"
- Embedded Resource: didn't work
- Resource: didn't work
我得到什么(什么):
我期待什么(东西):
,然后当你指向Internet Explorer访问资源(使用其 RES
协议):
And then when you point Internet Explorer at the resource (using its res
protocol):
res://C:\Develop\Avatar\LocaleInfo\LocaleInfo.exe\PNG\SplashPNG
IE浏览器可以找到它:
IE can find it:
推荐答案
管理资源嵌入到程序集从Win32的资源以不同的方式 - 嵌入式资源选项的会嵌入您的资源进入输出组件,但 不是在一个方式,都可以访问使用像的资源的协议。
Managed resources are embedded into assemblies in a different way from Win32 resources - the "Embedded resource" option will embed your resource into the output assembly, but not in a way that is accessible using things like the "res" protocol.
您可以使用一个工具来嵌入一个Win32到现有的资源,如下所述:的在C#程序中嵌入的Win32资源($ C $的CProject)。
You can either use a tool to embed a Win32 into an existing resource as described here: Embed Win32 resources in C# programs (CodeProject).
另外,您可以使用 / win32res
csc.exe的编译器选项嵌入编译 .RES
资源。此选项不是目前公开为在Visual Studio 2010的选项但是有一系列指令的此处,解释你如何能做到这一点。你只需要使用编译你的资源为正常 RC.EXE
(如为pre-生成步骤):
Alternatively you can use the /win32res
csc.exe compiler option to embed a compiled .res
resource. This option is not currently exposed as an option in Visual Studio 2010 however there is a series of instructions here that explains how you can do this. You simply need to compile your resource as normal using rc.exe
(e.g. as a pre-build step):
<Target Name="BeforeBuild" Inputs="my_resource_file.rc" Outputs="my_resource_file.res">
<Exec Command=""C:\Program Files\Microsoft Visual Studio 8\VC\bin\rc.exe" /r my_resource_file.rc" />
</Target>
,然后提供了 Win32Resource
属性来指定输出.RES文件:
And then supply the Win32Resource
property to specify the output .res file:
<Win32Resource>my_resource_file.res</Win32Resource>
更新:作为一种替代方法,您可以使用 RC MSBuild任务只要你不介意编辑的MSBuild您的.csproj文件。一个简单的例子:
Update: As an alternative you can use the RC MSBuild task as long as you don't mind editing MSBuild your .csproj file. A simple example:
<UsingTask TaskName="RC" AssemblyName="Microsoft.Build.CppTasks.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<PropertyGroup>
<Win32Resource Condition="'$(Win32Resource)' != ''">@(ResourceCompile->'%(RelativeDir)%(filename).res')</Win32Resource>
</PropertyGroup>
<ItemGroup>
<ResourceCompile Include="test.rc" />
</ItemGroup>
<Target Name="ResourceCompile" BeforeTargets="BeforeCompile" Condition="'@(ResourceCompile)' != ''">
<RC Source="@(ResourceCompile)" />
</Target>
如果安装了Visual C ++这仅仅工作。
This will only work if Visual C++ is installed.
这篇关于如何嵌入在.NET PE可执行文件的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!