【问题标题】:csproj property conditioncsproj 属性条件
【发布时间】:2011-03-15 11:38:32
【问题描述】:

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

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

有什么提示吗?

谢谢, 霍雷亚

【问题讨论】:

    标签: msbuild csproj


    【解决方案1】:

    您也许可以使用静态方法System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable

    不幸的是,我认为您不能直接从选择条件调用此静态方法来设置您的 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>
    

    【讨论】:

      【解决方案2】:

      我认为@Zach Bonham 的回答解决了一些不同的问题。我不知道我可以使用的静态函数有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>
      

      【讨论】:

        猜你喜欢
        • 2015-09-24
        • 2011-01-02
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多