![](/img/trans.png)
[英]How to remove computer from Domain (C# Code) and then add to workgroup?
[英]How can I get the Workgroup name of a computer using C#?
我已經閱讀過關於使用Environment類獲取它,但找不到它。
多謝你們。
一種基於Jono響應的方法,但更短:
public static string GetWorkGroup()
{
ManagementObject computer_system = new ManagementObject(
string.Format(
"Win32_ComputerSystem.Name='{0}'",
Environment.MachineName));
object result = computer_system["Workgroup"];
return result.ToString();
}
你可以用WMI做到這一點; 添加對System.Management.dll的引用和System.Management命名空間的using語句,然后調用以下代碼:
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\CIMV2", @"SELECT * FROM Win32_ComputerSystem"); foreach (ManagementObject mo in mos.Get()) { Console.WriteLine(mo["Workgroup"]); }
我嘗試使用這里建議的WMI選項,但事實證明我的機器(以及我辦公室中的其他幾個)的速度極慢(有時超過5秒)。 最終為我工作的是使用API調用“NetGetJoinInformation”( PInvoke.net )。 API調用對我來說非常快,並且完全符合我的需要。
在這里看一個例子。 你將不得不使用P / Invoke。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.