2012-10-07 13 views
1

我有一個Visual Studio項目,其啓動操作設置爲加載DLL在PowerShell中,像這樣:

<?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」。但是,這將評估爲空字符串。下面還決心爲空字符串:

  • $(的AssemblyName)
  • $(的TargetName)$(TargetExt)
  • $(TargetFileName)

設定值<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "$(Platform)" -PassThru | Select FullName</StartArguments>並創建commandline "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NoExit -Command Add-Type -Path "x64" -PassThru | Select FullName

回答

1

移動您的財產聲明後的所有導入指令,如<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

$(TargetPath)最初是在Microsoft.Common.targets中定義的,所以它將不包含任何值,直到該文件被導入到腳本中。如果您的屬性是在導入之前定義的(並且在導入的屬性出現之前) - 在自上而下的屬性評估階段,未定義的屬性將被解析爲空字符串。

注意:我錯誤地沒有注意到你的意思是proj * 。用戶 *文件,而不是原始的.proj文件。我不知道什麼時候visual Studio加載.user文件以及在加載期間設置了什麼上下文。 您可以嘗試使用原始.targets文件中的定義。這是TARGETDIR在Microsoft.common.targets如何定義的: <TargetDir Condition="'$(OutDir)' != ''">$([MSBuild]::Escape($([System.IO.Path]::GetFullPath( $([System.IO.Path] ::聯合收割機($(MSBuildProjectDirectory)$(OutDir)))))))</TargetDir>

。用戶如果加載文件沒有任何先前建立的上下文 - 它仍然將無法正確解析$(OutDir)。所以這仍然是不完整的回答你的問題=(

+0

我實際上正在通過Microsoft.common.targets尋找。也許只是重新導入它將工作。但是,我不希望$(TargetDir)我想要的名稱的DLL。 –

+0

這實際上取決於什麼樣的環境.user文件加載到VS啓動時。不幸的是,我找不到任何有關信息。我嚴重懷疑它會是相同的\相似的msbuild上下文用於生成項目。你可能會嘗試將你的原始.proj文件包含到.user中 - 然後它應該加載所有的導入,並且在評估階段之後,你應該在$(AssemblyName)中擁有合適的文件名,但是這是一種非常髒的方式,可以使用imo。 PS謝謝你的語法修復。 –