Visual Studio 中有一个文件夹,其中包含一些公共程序集。 VS2022 的示例:
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\PublicAssemblies
。
我有一个自定义的 MSBuild 项目,用于构建我的其他 .Net 项目(这是由于历史原因)。目前它有以下
AssemblySearchPaths: {TargetFrameworkDirectory};$(OutputDir);$(ExternalCustom);{GAC}
。正如您所看到的,它不包括提到的 PublicAssemblies
,因为我无法将此路径添加为硬编码路径。说我想知道是否有一个MSBuild property
持有这条路径。
不,没有内置或标准属性。你需要确定正确的路径并定义你自己的属性。
vswhere
工具,可用于确定 Visual Studio 安装目录。 vswhere
与 Visual Studio 一起安装时始终位于“Program Files (x86)”目录中的子目录“Microsoft Visual Studio\Installer”中。
Visual Studio安装目录的子文件夹结构稳定。您应该能够将
<install dir>\Common7\IDE\PublicAssemblies
用于 PublicAssemblies
目录。
使用 MSBuild 中的
vswhere
并定义属性的示例:
<Project>
<Target Name="Main" DependsOnTargets="GetVsInstallPath">
<Message Text="VisualStudioInstallPath: '$(VisualStudioInstallPath)'" />
<PropertyGroup>
<PublicAssembliesPath>$(VisualStudioInstallPath)\Common7\IDE\PublicAssemblies</PublicAssembliesPath>
</PropertyGroup>
<Message Text="PublicAssembliesPath: '$(PublicAssembliesPath)'" />
</Target>
<Target Name="GetVsInstallPath">
<PropertyGroup>
<!-- Enclose in quotes because of spaces in the directory names -->
<VsWhere Condition="'$(VsWhere)' == ''">"$(MSBuildProgramFiles32)\Microsoft Visual Studio\Installer\vswhere.exe"</VsWhere>
</PropertyGroup>
<Error Text="'$(VsWhere)' does not exist." Condition="Exists('$(VsWhere)')" />
<Exec Command="$(VsWhere) -latest -property installationPath" ConsoleToMSBuild="true" EchoOff="true" StandardOutputImportance="Low">
<Output TaskParameter="ConsoleOutput" PropertyName="VisualStudioInstallPath" />
</Exec>
</Target>
</Project>
同一台计算机上可以安装多个 Visual Studio。
-latest
开关将 vswhere
的输出限制为“仅最新版本和最后安装的版本”。如果您希望考虑 Visual Studio 的预览版本,请添加 -prerelease
开关。 (在撰写本文时,VS2022 的发布版本最高支持 .NET 7,VS2022 的预览版本支持 .NET 8.0 的预发布。)
-property installationPath
参数将vswhere
的输出限制为installationPath
,并且整个输出可以用作VisualStudioInstallPath
属性的值。