gpt4 book ai didi

c# - 如何以跨平台方式使用 .NET 5 获取硬件信息?

转载 作者:行者123 更新时间:2023-12-04 17:22:15 28 4
gpt4 key购买 nike

我正在开发一个 .NET 5 控制台应用程序,并希望能够读取一些基本的系统信息,例如 CPU 温度、内存使用率、CPU 使用率等。我的开发机器正在运行 Windows,但该应用程序已部署到Linux 机器,因此我想避免为两个操作系统编写两组不同的代码。

我如何以跨平台方式执行此操作?有很多 Windows API 和调用可以执行此操作,但我也需要 Linux 才能工作。

最佳答案

有一个 github 问题开放,将其标准化为 .net 运行时的一部分。也许我们会在 .net 6 或 7 中看到这一点,谁知道呢... https://github.com/dotnet/runtime/issues/22948

这是更好的库之一:https://github.com/openhardwaremonitor/openhardwaremonitor .他们似乎尝试了一些 Linux 支持。

对于诸如 CPU 使用率之类的事情,您可能会成功使用 How to get the CPU Usage in C#? ( Windows )。

对于Linux上的CPU,命令行解析结果:https://phoenixnap.com/kb/check-cpu-usage-load-linux .简而言之:

sudo apt install sysstat
mpstat 1

GC 类具有获取 C# 运行时使用的内存的方法。对于系统内存,这里有一些资源:
Windows :How do you get total amount of RAM the computer has?
Linux:运行 free -m 并使用 Process 类解析结果。

其他可能对 Linux 有帮助的链接:

关于c# - 如何以跨平台方式使用 .NET 5 获取硬件信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65522760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com