Find out the “Bit”ness of the current OS in MSBuild

空扰寡人 提交于 2019-12-05 07:01:19

There is a registry key that will tell you the bit-edness of the current OS. Here are the properties I use in my MSBuild files:

<PropertyGroup>
        <MachineProcessorArchitecture>$(registry:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment@PROCESSOR_ARCHITECTURE)</MachineProcessorArchitecture>
        <Is32Bit>False</Is32Bit>
        <Is32Bit Condition="'$(MachineProcessorArchitecture)' == 'x86'">True</Is32Bit>
        <Is64Bit>False</Is64Bit>
        <Is64Bit Condition="'$(MachineProcessorArchitecture)' == 'AMD64'">True</Is64Bit>
</PropertyGroup>

On a 64-bit OS, the following variables are defined:

ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)

So just test for ProgramFiles(x86) and if it's empty, use ProgramFiles.

You're using the bitness to try and guess the correct Program Files folder, but there's no guarantee that it's on the C drive, or even called "Program Files". You would be better using the $(MSBuildProgramFiles32) property (in MSBuild 4.0).

If you're always running the 32-bit version of MSBuild, regardless of the platform, then it's easy: just substitute '$(ProgramFiles)' for 'C:\Program Files'. Whether on a 32-bit or 64-bit OS, '$(ProgramFiles)' should expand to the correct folder location (the location of all 32-bit programs).

If you're running the 64-bit version of MSBuild on 64-bit platforms (which is unlikely), then it gets a bit trickier. The '%ProgramFiles(x86)%' environment variable would seem to be what you want, but good luck dealing with those parentheses. Easier would probably be to use the '%PROCESSOR_ARCHITECTURE%' environment variable in a condition.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!