2011-03-15 2 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 Bonham의 anwer가 조금 다른 문제를 해결한다고 생각합니다. 내가 사용할 수있는 정적 함수에 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> 

관련 문제