如何使用 C# 获取计算机的工作组名称?

发布于 2024-08-30 08:24:15 字数 44 浏览 0 评论 0原文

我读过有关通过环境类获取它的信息,但找不到它。

谢谢你们。

I've read about getting it with the Environment class, but can't find it.

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友谊不毕业 2024-09-06 08:24:15

一种基于 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();
}

A way based on the Jono response, but shorter:

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();
}
游魂 2024-09-06 08:24:15

我使用此处建议的 WMI 选项尝试了此操作,但结果在我的计算机(以及我办公室的其他几台计算机)上速度非常慢(有时超过 5 秒)。最终对我有用的是使用 API 调用“NetGetJoinInformation”(PInvoke.net)。 API 调用对我来说返回得非常快,并且完全满足了我的需要。

I tried this using the WMI options suggested here, but it turned out to be excruciatingly slow (sometimes over 5 seconds) on my machine (and several others in my office). What ended up working for me was using the API call "NetGetJoinInformation" (PInvoke.net). The API call returns very quickly for me and does exactly what I need.

岛徒 2024-09-06 08:24:15

您可以使用 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"]);
}

You can do this with WMI; add a reference to System.Management.dll and a using statement for System.Management namespace, then call the following code:

ManagementObjectSearcher mos = 
  new ManagementObjectSearcher(@"root\CIMV2", @"SELECT * FROM Win32_ComputerSystem");
foreach (ManagementObject mo in mos.Get()) {
  Console.WriteLine(mo["Workgroup"]);
}
梦境 2024-09-06 08:24:15

请参阅此处查看示例。您将必须使用 P/Invoke。

Look here for an example. You will have to use P/Invoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文