Msbuild csproj财产状况

Msbuild csproj财产状况,msbuild,csproj,Msbuild,Csproj,我想在csproj文件中添加一个带有条件的属性 条件是:如果网络位置可用,则我的属性应具有该值,否则为其他位置 有什么提示吗 谢谢, Horea您可能可以使用静态方法 不幸的是,我认为您不能直接从所选条件调用此静态方法来设置PropertyGroup。您可能需要编写一个自定义内联MSBuild任务来完成此任务 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="h

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

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

有什么提示吗

谢谢,
Horea

您可能可以使用静态方法

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

你已经接通了
您没有连接

IsConnected=System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable().ToString();

你已经接通了

我认为@Zach Bonham的anwer解决了一个稍微不同的问题。我不知道我可以使用的静态函数有限制,但没有包括在内。因此,有必要使用@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>


Exists=System.IO.Directory.Exists(DirectoryPath);

\\192.168.1.1\some\path
\\127.0.0.1\默认\位置
$(网络位置)
$(DefaultNetworkLocation)