2
A
回答
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
我認爲@Zach文翰的前面回答解決了一個有點不同的問題。我不知道我可以使用的靜態函數有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>
相關問題
- 1. Wix:財產狀況不起作用
- 2. 財政狀況實施
- 3. INotifyPropertyChanged財產財產
- 4. 處理影響另一個財產狀態的財產
- 5. 獲取財產的財產
- 6. Maven查找財產使用情況
- 7. Laravel隊列 - 記住財產狀態?
- 8. 財產
- 9. 財產
- 10. 財產
- 11. 將財產委託給其他財產
- 12. 綁定Checkbox.Checked財產財產上的DataSet
- 13. 要財產,還是不要財產?
- 14. Highcharts計財產
- 15. 勢必財產
- 16. JSON財產
- 17. CSS - 財產
- 18. 財產繼承
- 19. MaxReceivedMessageSize財產
- 20. background-size:cover;財產
- 21. javax.servlet.jsp.JspException:無財產
- 22. dotencode財產
- 23. 財產3屆
- 24. modifiy項財產
- 25. 基於財產
- 26. 火箭財產
- 27. 財產在angular2
- 28. 的NSDictionary財產
- 29. SqlMembershipProvider.ApplicationName財產
- 30. 虛擬財產