csproj 财产状况

发布于 2024-10-22 18:45:50 字数 114 浏览 4 评论 0原文

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

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

有什么提示吗?

谢谢, 霍雷亚

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 value, otherwise another location.

Any hint ?

thanks,
Horea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女 2024-10-29 18:45:50

您也许可以使用静态方法 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>

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>
太傻旳人生 2024-10-29 18:45:50

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

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文