csprojファイルに条件付きのプロパティを追加したいと考えています。
条件は次のとおりです。ネットワークの場所が利用可能な場合、私のプロパティはその値を持つ必要があり、そうでない場合は別の場所です。
ヒントはありますか?
ありがとう、ホレア
静的メソッド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>
@Zach Bonhamの回答は、少し異なる問題を解決すると思います。使用できる静的関数に制限があり、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>