如何使用.Net Core识别Linux/Mac计算机的硬件详细信息 [英] How to identify the hardware details of a Linux/Mac machine using .Net Core

查看:62
本文介绍了如何使用.Net Core识别Linux/Mac计算机的硬件详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用.Net Core识别Linux/Mac计算机的硬件详细信息.

How to identify the hardware details of a Linux/Mac machine using.Net Core.

对于Windows计算机,我们可以使用 System.Management 和WMI查询.

For windows machines, we can use System.Management and WMI Query.

因此,可以使用任何类似的方法来识别Linux和Mac计算机的硬件详细信息(例如RAM,处理器,监视器,CAM等).

So is there any similar way to identify the hardware details (like RAM ,Processor,Monitor ,CAM etc) of Linux and Mac machines.

对于Windows,我正在使用:

For windows, I'm using:

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher("select * from Win32_Processor");

推荐答案

我已经完成了一种变通方法,可以根据平台来获取硬件信息.对于Windows,我使用了旧的系统管理类,对于Linux,我使用了不同的Bash命令来获取处理器ID,型号,模型版本,机器ID.以下是我正在使用的一些Linux命令

I have done a workaround to get hardware info as per Platform. For windows I have used old way of system Management classes, for Linux i have used different Bash commands to Get Processor Id, Model,model version,machine id. Following are some linux commands i am using

1. "LinuxModel": "cat /sys/class/dmi/id/product_name"
2. "LinuxModelVersion": "cat /sys/class/dmi/id/product_version"
3. "LinuxProcessorId": "dmidecode -t processor | grep -E ID | sed 's/.*: //' | head -n 1"
4. "LinuxFirmwareVersion": "cat /sys/class/dmi/id/bios_version",
5. "LinuxMachineId": "cat /var/lib/dbus/machine-id"

很快就会在.net核心框架中寻求支持

Waiting for some support in the .net core framework soon

我的gihub帖子地址为 https://github.com/dotnet/corefx/issues/22660

My gihub post address is https://github.com/dotnet/corefx/issues/22660

我还使用了类似的扩展方法,并对bash命令进行了一些优化的代码

I have also used similar extension method with a bit optimized code for bash command

public static string Bash(this string cmd)
        {
            string result = String.Empty;

            try
            {
                var escapedArgs = cmd.Replace("\"", "\\\"");

                using (Process process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        FileName = "/bin/bash",
                        Arguments = $"-c \"{escapedArgs}\"",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true,
                    };

                    process.Start();
                    result = process.StandardOutput.ReadToEnd();
                    process.WaitForExit(1500);
                    process.Kill();
                };
            }
            catch (Exception ex)
            {
                //Logger.ErrorFormat(ex.Message, ex);
            }
            return result;
        }

这篇关于如何使用.Net Core识别Linux/Mac计算机的硬件详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆