csproj财产状况

时间:2011-03-15 11:38:32

标签: msbuild csproj

我想将一个带有条件的属性添加到csproj文件中。

条件是:如果网络位置可用,我的属性应具有该值,否则为另一个位置。

任何提示?

感谢, Horea

2 个答案:

答案 0 :(得分: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 :(得分: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>