Ian*_*oyd 5 .net resources visual-studio-2010 visual-studio
如何在Visual Studio 2010中的.NET PE(可移植可执行文件)中包含资源?
在过去,我们将创建一个资源脚本文件:
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"
Run Code Online (Sandbox Code Playgroud)
将该文件添加到项目中,编译器将编译该.rc
文件; 包括最终可执行映像中的资源.
什么是托管/ .NET/Visual Studio机制来包含资源?
这些必须是标准资源; 你知道每个人都可以读作资源的那种:
res
协议读取(例如 res://c:\foo\MyProgram.exe/PNG/SplashLogo
)我试过的东西不起作用:
向Resources.resx
文件添加资源:
向Resources.resx
文件添加资源,并指定Resource的构建操作:
(也尝试了构建行动:嵌入式资源,正如我在2008年向我建议的那样)
更新:什么不起作用
我尝试将一个文件(wumpa.rc
)添加到项目中:
wumpa.rc:
SplashPNG PNG "Splash.png"
Run Code Online (Sandbox Code Playgroud)
默认情况下它不起作用.我试图改变生成操作的wumpa.rc
:
我得到了什么(没有):
我期待什么(某事):
然后当您将Internet Explorer指向资源时(使用其res
协议):
res://C:\Develop\Avatar\LocaleInfo\LocaleInfo.exe\PNG\SplashPNG
Run Code Online (Sandbox Code Playgroud)
IE可以找到它:
托管资源以与Win32资源不同的方式嵌入到程序集中 - "嵌入式资源"选项将您的资源嵌入到输出程序集中,但不能以使用"res"协议之类的方式访问.
您可以使用工具将Win32嵌入到现有资源中,如下所述:在C#程序(CodeProject)中嵌入Win32资源.
或者,您可以使用/win32res
csc.exe编译器选项嵌入已编译的.res
资源.该选项目前未暴露在Visual Studio 2010中的一个选项但是有一系列的指令在这里,解释你如何能做到这一点.您只需使用正常编译资源rc.exe
(例如,作为预构建步骤):
<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>
Run Code Online (Sandbox Code Playgroud)
然后提供Win32Resource
属性以指定输出.res文件:
<Win32Resource>my_resource_file.res</Win32Resource>
Run Code Online (Sandbox Code Playgroud)
更新:作为替代方案,您可以使用RC MSBuild任务,只要您不介意编辑MSBuild .csproj文件即可.一个简单的例子:
<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>
Run Code Online (Sandbox Code Playgroud)
这只适用于安装Visual C++的情况.