[英]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.