开发者

csproj property condition

i would like to add a property with a condition to the csproj file.

condition is: if a network location is available, my property should have that va开发者_开发知识库lue, otherwise another location.

Any hint ?

thanks, Horea


You might be able to use the static method System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable.

Unfortunately, I don't think you can invoke this static method directly from a Chose condition to set your PropertyGroup. You may need to write a custom inline MSBuild task to do this for you.

<?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>


I think that @Zach Bonham's anwer solves a bit different problem. I didn't know that there is exists limitation on static functions that I can use and File.Exists is included, but Directory.Exists is not included. So there is exists necessity to use custom task like proposed one by @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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜