我可以通过哪些方式通过编程接口查询本地计算机的规格(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.
发布评论
评论(6)
也许考虑使用 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.
对于此类信息,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.
我不妨发布我用来获取所需一切的基本代码,按照此处的建议使用 WMI。
必须在 c# 项目中包含对 System.Management 的引用。 然后,源代码本身可能是非常糟糕的 C# 形式,但我以前从未真正用它编写过,而且它是用于内部工具的,所以这有点不重要。 我对其进行了一些简化,因此它仅真正处理输出机器规格(除此之外,该工具还执行其他操作)。 每次调用 LogClass() 都会转储其所有属性。 要转储更多类,只需查看 MSDN 部分 WMI 类。
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.
对于您要为 Linux 进行开发的奇怪情况,您可以在
/proc
伪文件系统中找到很棒的东西。For the odd case that you would be developing for linux, you could find awesome stuff inside the
/proc
pseudofilesystem.如果您决定使用 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.
有一个名为
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