2011-03-15 63 views
2

我想添加一個條件屬性到csproj文件。csproj財產狀況

條件是:如果一個網絡位置是可用的,我的屬性應該有值,否則另一個位置。

任何提示?

感謝, Horea

回答

6

您可能能夠使用靜態方法System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable

不幸的是,我不認爲你可以直接從Chose條件調用這個靜態方法來設置你的PropertyGroup。你可能需要編寫一個自定義的內聯MSBuild任務來爲你做這件事。

<?xml version="1.0" encoding="utf-8"?> 
<Project 
    ToolsVersion="4.0" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
    InitialTargets="Test" 
    DefaultTargets="Test" 
    > 
     <Choose> 
     <When Condition="$(IsConnected) == 'True'"> 
      <PropertyGroup> 
       <ConnectMessage>You are connected</ConnectMessage> 
      </PropertyGroup> 
     </When> 

     <Otherwise> 
      <PropertyGroup> 
       <ConnectMessage>You are NOT connected</ConnectMessage> 
      </PropertyGroup> 
     </Otherwise> 

     </Choose> 


     <UsingTask 
     TaskName="GetConnectionStatus" 
     TaskFactory="CodeTaskFactory" 
     AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> 

     <ParameterGroup> 
      <IsConnected ParameterType="System.String" Output="true" /> 
     </ParameterGroup> 
     <Task> 
      <Code Type="Fragment" Language="cs"> 
      IsConnected = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable().ToString(); 
      </Code> 
     </Task> 
     </UsingTask> 


    <Target Name="Initialize"> 

     <GetConnectionStatus> 
      <Output PropertyName="IsConnected" TaskParameter="IsConnected" /> 
     </GetConnectionStatus> 

     <PropertyGroup> 
      <ConnectMessage Condition="$(IsConnected) == 'True'">You Are Connected</ConnectMessage> 
     </PropertyGroup> 

     <Message Text="ConnectionStatus $(IsConnected)"/> 
     <Message Text="$(ConnectMessage)"/> 
    </Target> 

    <Target Name="Test" DependsOnTargets="Initialize"> 

     <Message Text="$(ConnectMessage)"/> 

    </Target> 
</Project> 
1

我認爲@Zach文翰的前面回答解決了一個有點不同的問題。我不知道我可以使用的靜態函數有exists限制,並且包含File.Exists,但不包括Directory.Exists。所以有必要使用@Zach Bonham提出的自定義任務。

<?xml version="1.0" encoding="utf-8"?> 
<Project 
    ToolsVersion="4.0" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
    InitialTargets="SetLocation" 
    > 

    <UsingTask 
    TaskName="IsDirectoryExists" 
    TaskFactory="CodeTaskFactory" 
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> 

    <ParameterGroup> 
     <Exists ParameterType="System.Boolean" Output="true" /> 
     <DirectoryPath Required="true" ParameterType="System.String" /> 
    </ParameterGroup> 
    <Task> 
     <Code Type="Fragment" Language="cs"> 
     Exists = System.IO.Directory.Exists(DirectoryPath); 
     </Code> 
    </Task> 
    </UsingTask> 

    <PropertyGroup> 
    <NetworkLocation>\\192.168.1.1\some\path</NetworkLocation> 
    <DefaultNetworkLocation>\\127.0.0.1\default\location</DefaultNetworkLocation> 
    </PropertyGroup> 

    <Target Name="SetLocation"> 
    <IsDirectoryExists DirectoryPath="$(NetworkLocation)"> 
     <Output PropertyName="NetworkLocationExists" TaskParameter="Exists" /> 
    </IsDirectoryExists> 

    <PropertyGroup> 
     <UseLocation Condition="'$(NetworkLocationExists)'=='true'">$(NetworkLocation)</UseLocation> 
     <UseLocation Condition="'$(UseLocation)'==''">$(DefaultNetworkLocation)</UseLocation> 
    </PropertyGroup> 

    <Message Text="NetworkLocationExists: $(NetworkLocationExists)" /> 
    <Message Text="UseLocation: $(UseLocation)" /> 
    </Target>