c# 工程加入 win32 资源文件
这种事情就是一个字烦人,蛋疼,特别蛋疼。 搞了我一天。
方法有几种,都特别烦,一个是 build event , 一个是 msbuild
今天说 build event
1.在 Build Events 中 , Pre-build
输入:
copy $(ProjectDir)Time/norlib.Time.Holidays.xml "$(TargetDir)norlib.Time.Holidays.xml" set VCInclude=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include set SDKInclude=C:\Program Files (x86)\Windows Kits\8.0\Include\um set SDKShared=C:\Program Files (x86)\Windows Kits\8.0\Include\shared echo $(IntermediateOutputPath) set RCDIR= IF EXIST "C:\Program Files (x86)\Windows Kits\8.0\bin\x86\rc.exe" (set RCDIR="C:\Program Files (x86)\Windows Kits\8.0\bin\x86\rc.exe") echo (%RCDIR% /i "%VCInclude%" /i "%SDKInclude%" /r /fo "$(IntermediateOutputPath)\res1.res" "$(ProjectDir)Controls\OpenFileOrFolderDialog\res1.rc") if not defined RCDIR ( echo "Error! Unable to find rc.exe.") else (%RCDIR% /i "%VCInclude%" /i "%SDKInclude%" /i "%SDKShared%" /r /fo "$(ProjectDir)res1.res" "$(ProjectDir)Controls\OpenFileOrFolderDialog\res1.rc") if not defined RCDIR (Exit 1)
主要是从第二行开始看
首先要注意 c# 下面很多宏是失效的, 比如 $(VCInstallDir) , 还有写宏和 vc 是指向不一样的目录,比如他妈的: $(FrameworkSdkDir)
这个宏 $(FrameworkSdkDir) 在vc下 指向: C:\program files(x86)\windows kits\8.0\ 在里面bin\x86 我们可以找到rc.exe
但是在 c# 下指向:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\
瞬间晕死, google了半天发现个帖子:
http://go4answers.webhost4life.com/Example/get-winsdk-configuration-tool-work-41266.aspx
他这么说的:
After some investigation and reflecting using Redgate Reflector, I found that the variable $(FrameworkSDKDir) will always resolve to the 6.0a installation if that SDK is installed and the target framework is 3.5. This is due to a possible bug in the .NET Type Microsoft.Build.Shared.FrameworkLocationHelper, in assembly Microsoft.Build.Utilities.v3.5, Version=3.5.0.0 that shipped with .NET Framework 3.5. The problem I see is in the property PathToDotNetFrameworkSdkV35 in this class. It first reads the registry value for SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A\InstallationFolder, while I believe it should actually first read SOFTWARE\Microsoft\Microsoft SDKs\Windows\CurrentInstallFolder. I worked around this problem by hacking the key SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A\InstallationFolder to point to 6.1. This enables me to use rc.exe from SDK 6.1, while it might affect other products that expects to retrieve the 6.0a installation folder from the registry. I have the following questions regarding the issues described above: Why would rc.exe install to different locations on two otherwise seemingly equivalent machines? Is the code that I refered to in Microsoft.Build.Shared.FrameworkLocationHelper affected by a bug? and if so Is there a work around / fix other that the method that I described?
反正要解决也挺麻烦的,后来如你所见我就在 build events 里面创建了一个 %Macro%
比如:
set VCInclude=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\atlmfc\include
这里要注意了,你不能写 VCInclude =....... 这样Macro变成了 %VCInclude % .........还有就是不需要双引号。
这样编译成功后, 就有了一个 res 文件,我们千辛万苦就是为了得到这个res文件
然后 c# Project Property -> Application -> Resources -> Resource file (radio button): 输入res文件的绝对路径
这样就能加入了win32资源文件了
里面挺好玩的。
参考:
http://stackoverflow.com/questions/8057080/how-to-embed-a-resource-in-a-net-pe-executable
http://blogs.msdn.com/b/astebner/archive/2006/02/28/541036.aspx
以后还是要上MSBUILD
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能