查询机器规格

发布于 2024-07-07 10:23:59 字数 629 浏览 10 评论 0 原文

我可以通过哪些方式通过编程接口查询本地计算机的规格(CPU 规格、操作系统版本、显卡规格和驱动程序等一系列信息)? 我们正在用 C# 编写一个简单的应用程序来测试主应用程序的兼容性,并希望它转储一些系统指标,但我似乎找不到从哪里开始,使用什么接口,库,任何东西。

我尝试过各种搜索,但只能找到需要用户交互或必须安装的程序,以及 GUI 程序。

或者,一个小型的命令行程序也可以工作,只要我们被允许将它与测试应用程序一起分发即可。

我发现一个程序可以满足我想要的一些规格,PsInfo< /a>. 然而,它似乎要求每个用户在首次运行时同意某些许可证,即使它是一个命令行应用程序。 另外,它只处理操作系统/CPU 信息,我需要的不仅仅是这些。

另外:忘记明确提及,但这确实只适用于 Windows 机器。 你们可真快啊!

编辑:这个 WMI 确实看起来像我需要的,谢谢! 不过,这确实是一大堆蠕虫,所以我必须深入研究。它提到,对于某些事情,用户必须拥有管理员权限; 这可能不会是一个大问题,但可能会有点限制。

What are some ways that I can query the local machine's specifications (a range of things from CPU specs, OS version, graphics card specs and drivers, etc.) through a programmatic interface? We're writing a simple app in C# to test compatibility of our main app and want to have it dump out some system metrics, but I can't seem to find where to even start, what interfaces to use, libraries, anything.

I've tried all kinds of searches, but can only find programs, and GUI ones at that, which require a user to interact with, or have to install.

Alternatively, a small, command-line program would work just as well, as long as we'd be permitted to distribute it with the test app.

I have found one program that gets some of the specs I'd want, PsInfo. However, it seems to require each user to agree to some license when it is first run, even though it's a command line app. Plus, it only deals with OS/CPU info, and I will need more than that.

Also: forgot to mention explicitly, but this indeed is only going to be necessary for Windows machines. You folks are quick!

Edit: This WMI does look like what I need, thanks! Quite a can of worms though, so I've got to dive in. It mentions that for some things the user has to have administrator privileges; this probably won't be a big problem, but it might limit it a little.

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

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

发布评论

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

评论(6

葬シ愛 2024-07-14 10:23:59

也许考虑使用 Windows Management Instrumentation (WMI ),假设您计划查询的是 Windows 计算机。 查看 WMI来自 Microsoft 的代码创建器。 它可以轻松帮助您使用几种语言的代码示例。

WMI 在 Windows 2000 时代一直表现出色,但在一些帮助下也可以在 Win98 机器上工作。

只要您拥有或可以提供您要查询的计算机的管理员凭据,WMI 绝对是您的最佳选择。

Maybe look into using Windows Management Instrumentation (WMI), assuming it's a Windows machine your planning to query. Take a look at the WMI Code Creator from Microsoft. It can easily get you going with code samples for in a few languages.

WMI works excellent all the way back to the Windows 2000 era, but can also work in Win98 machines too with some help.

As long as you have, or can provide, administrator credentials for the machine you're trying to query, WMI is definitely the way to go.

臻嫒无言 2024-07-14 10:23:59

对于此类信息,WMI 是您的朋友。 幸运的是,在 .NET 中处理 WMI 比在非托管世界中容易得多。 有很多文章可供入门,例如 这篇,或这个检索处理器信息。

您最终将针对 WMI 命名空间中的对象编写类似 SQL 的查询来检索所需的信息。

For this type of information WMI is your friend. Fortunately dealing with WMI in .NET is much easier than in the unmanaged world. There are quite a lot of articles out there to get started with, like this one, or this one to retrieve processor information.

You will end up writing SQL-like queries against objects in the WMI namespace to retrieve the information you want.

泛泛之交 2024-07-14 10:23:59

我不妨发布我用来获取所需一切的基本代码,按照此处的建议使用 WMI。

必须在 c# 项目中包含对 System.Management 的引用。 然后,源代码本身可能是非常糟糕的 C# 形式,但我以前从未真正用它编写过,而且它是用于内部工具的,所以这有点不重要。 我对其进行了一些简化,因此它仅真正处理输出机器规格(除此之外,该工具还执行其他操作)。 每次调用 LogClass() 都会转储其所有属性。 要转储更多类,只需查看 MSDN 部分 WMI 类

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;

namespace SyTest
{
  class Program
  {
    static StreamWriter specStream;

    static void Main(string[] args)
    {
      FileStream specFile =
          new FileStream("machine-specs.txt",FileMode.Create,FileAccess.Write);
      specStream = new StreamWriter(specFile);

      LogClass("Win32_DesktopMonitor");
      LogClass("Win32_VideoController");
      LogClass("Win32_Processor");
      // etc

      specStream.Close();
      specFile.Close();
    }

    static void LogClass(string strTable)
    {
      if (strTable.Length <= 0) return;
      specStream.Write("--- " + strTable + " ---\r\n\r\n");
      WqlObjectQuery wqlQuery =
          new WqlObjectQuery("SELECT * FROM " + strTable);
      ManagementObjectSearcher searcher =
          new ManagementObjectSearcher(wqlQuery);
      try
      {
        if (searcher.Get().Count <= 0)
        {
          specStream.Write("Class has no instances\r\n\r\n");
        }
        foreach (ManagementObject obj in searcher.Get())
        {
          specStream.Write("* " + obj.ToString() + "\r\n");

          if (obj.Properties.Count <= 0)
          {
            specStream.Write("Class instance has no properties\r\n");
            continue;
          }

          foreach (System.Management.PropertyData prop in obj.Properties)
          {
            LogAttr(obj, prop.Name);
          }

          specStream.Write("\r\n");
        }
      }
      catch { specStream.Write("Class does not exist\r\n\r\n"); }
    }
    static void LogAttr(ManagementObject obj, string str)
    {
      if (str.Length <= 0) return;
      string strValue = "";
      try
      {
        strValue = obj[str].ToString();
        try
        {
          string[] pstrTmp = ((string[])obj[str]);
          if (pstrTmp.Length > 0) strValue = String.Join(", ", pstrTmp);
        }
        catch { } // Problem casting, fall back on original assignment
      }
      catch { strValue = "[UNDEFINED]"; }
      specStream.Write(str + ": " + strValue + "\r\n");
    }
  }
}

I might as well post the basic code I used to get everything I needed, using WMI as was suggested here.

Gotta include a reference to System.Management in the c# project. Then, the source itself is probably terribly formed c#, but I've never really written in it before and it's for an internal tool, so that's kind of besides the point. I've simplified it a bit so it's only really dealing with outputting machine specs (the tool does other things besides that). Each call to LogClass() dumps out all of its properties. For more classes to dump out, just check out the MSDN section on WMI Classes.

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;

namespace SyTest
{
  class Program
  {
    static StreamWriter specStream;

    static void Main(string[] args)
    {
      FileStream specFile =
          new FileStream("machine-specs.txt",FileMode.Create,FileAccess.Write);
      specStream = new StreamWriter(specFile);

      LogClass("Win32_DesktopMonitor");
      LogClass("Win32_VideoController");
      LogClass("Win32_Processor");
      // etc

      specStream.Close();
      specFile.Close();
    }

    static void LogClass(string strTable)
    {
      if (strTable.Length <= 0) return;
      specStream.Write("--- " + strTable + " ---\r\n\r\n");
      WqlObjectQuery wqlQuery =
          new WqlObjectQuery("SELECT * FROM " + strTable);
      ManagementObjectSearcher searcher =
          new ManagementObjectSearcher(wqlQuery);
      try
      {
        if (searcher.Get().Count <= 0)
        {
          specStream.Write("Class has no instances\r\n\r\n");
        }
        foreach (ManagementObject obj in searcher.Get())
        {
          specStream.Write("* " + obj.ToString() + "\r\n");

          if (obj.Properties.Count <= 0)
          {
            specStream.Write("Class instance has no properties\r\n");
            continue;
          }

          foreach (System.Management.PropertyData prop in obj.Properties)
          {
            LogAttr(obj, prop.Name);
          }

          specStream.Write("\r\n");
        }
      }
      catch { specStream.Write("Class does not exist\r\n\r\n"); }
    }
    static void LogAttr(ManagementObject obj, string str)
    {
      if (str.Length <= 0) return;
      string strValue = "";
      try
      {
        strValue = obj[str].ToString();
        try
        {
          string[] pstrTmp = ((string[])obj[str]);
          if (pstrTmp.Length > 0) strValue = String.Join(", ", pstrTmp);
        }
        catch { } // Problem casting, fall back on original assignment
      }
      catch { strValue = "[UNDEFINED]"; }
      specStream.Write(str + ": " + strValue + "\r\n");
    }
  }
}
蓦然回首 2024-07-14 10:23:59

对于您要为 Linux 进行开发的奇怪情况,您可以在 /proc 伪文件系统中找到很棒的东西。

For the odd case that you would be developing for linux, you could find awesome stuff inside the /proc pseudofilesystem.

羅雙樹 2024-07-14 10:23:59

如果您决定使用 WMI,您可能需要查看 WMI 代码创建器。 它使得处理 WMI 变得非常容易。

If you decide to go with WMI, you might want to check out the WMI Code Creator from Microsoft. It makes dealing with WMI pretty easy.

忆依然 2024-07-14 10:23:59

有一个名为 MissingLinq.Linq2Management 的 nuget 包,它已将有关 WMI 的几乎所有内容包装到一个漂亮的强类型对象中。 看起来很不错。

https://www.nuget.org/packages/MissingLinq.Linq2Management

There is a nuget package called MissingLinq.Linq2Management that has wrapped pretty much everything about WMI into a nice strongly typed object. Seems pretty nice.

https://www.nuget.org/packages/MissingLinq.Linq2Management

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