我們正在開發需要了解Microsoft DirectAccess連接狀態的.NET應用程序。我知道可以通過PowerShell命令Get-DAConnectionStatus
查詢狀態。適用於DirectAccess狀態的.NET API
但是,是否也可以通過託管.NET庫/ API查詢此DirectAccess狀態?解決方法是從C#運行PowerShell腳本,但我認爲從性能角度來看這並不理想(我們需要定期檢查這個狀態)。
我們正在開發需要了解Microsoft DirectAccess連接狀態的.NET應用程序。我知道可以通過PowerShell命令Get-DAConnectionStatus
查詢狀態。適用於DirectAccess狀態的.NET API
但是,是否也可以通過託管.NET庫/ API查詢此DirectAccess狀態?解決方法是從C#運行PowerShell腳本,但我認爲從性能角度來看這並不理想(我們需要定期檢查這個狀態)。
我能想象以下(不testet尚未雖然):
您可以通過
Get-CimInstance -Namespace "root/StandardCimv2" MSFT_DAConnectionStatus
查詢相同的信息,您可以通過爲Windows SDK
裝配Microsoft.Management.Infrastructure
查詢由C#CIM
實例解釋here
最終我想出了這個C#代碼,該代碼適用於帶DirectAccess和不帶DirectAccess的機器:
/// <summary>
/// </summary>
/// <returns>-1 if DA is not available on this system.</returns>
private int GetDAStatus()
{
int status = -1;
string NamespacePath = @"\\.\ROOT\StandardCimv2";
string ClassName = "MSFT_DAConnectionStatus";
try
{
ManagementClass oClass = new ManagementClass(NamespacePath + ":" + ClassName);
foreach (ManagementObject mo in oClass.GetInstances())
{
status = Convert.ToInt32(mo["Status"]);
break;
}
}
catch (System.Management.ManagementException mex)
{
if (mex.ErrorCode == ManagementStatus.InvalidNamespace)
{
status = -1;
}
else throw;
}
return status;
}
'///
你是對的user9993,我確實提供了一個公共'get'屬性:IsDirectAccessEnabled()調用GetDAStatus()。私有函數未公開,但在別處用於訪問真正的DirectAccess狀態(如果可用)。 –
這似乎是你想要查詢一次,當你配置你的服務器,之後不再這樣編寫代碼返回狀態似乎是不需要的東西(國際海事組織)。也許我錯了? – Igor