我有一个 Visual Studio 项目,其启动操作设置为在 PowerShell 中加载 DLL,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Platform)' == 'Debug|x86'">
<StartAction>Program</StartAction>
<StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "Foo.dll" -PassThru | Select FullName</StartArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<StartAction>Program</StartAction>
<StartProgram>c:\windows\system32\windowspowershell\v1.0\powershell.exe</StartProgram>
<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "Foo.dll" -PassThru | Select FullName</StartArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "Foo.dll" -PassThru | Select FullName</StartArguments>
<StartAction>Program</StartAction>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<StartProgram>c:\windows\system32\windowspowershell\v1.0\powershell.exe</StartProgram>
<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "Foo.dll" -PassThru | Select FullName</StartArguments>
<StartAction>Program</StartAction>
</PropertyGroup>
</Project>
我将文件手动编辑为以下内容以消除冗余:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup '$(Platform)' == 'x86'">
<StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)' == 'AnyCPU' or Condition="'$(Platform)' == 'x64'">
<StartProgram>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
</PropertyGroup>
<PropertyGroup>
<StartAction>Program</StartAction>
<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "Foo.dll" -PassThru | Select FullName</StartArguments>
</PropertyGroup>
</Project>
现在我想用“$(TargetPath)”之类的东西替换硬编码的“Foo.dll”。但是,计算结果为空字符串。以下也解析为空字符串:
- $(程序集名称)
- $(TargetName)$(TargetExt)
- $(目标文件名)
设置值<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "$(Platform)" -PassThru | Select FullName</StartArguments>
确实会创建命令行"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NoExit -Command Add-Type -Path "x64" -PassThru | Select FullName