简体   繁体   English

在C#中确定操作系统和处理器类型

[英]Determine operating system and processor type in C#

I want to check what type of operating system i use and what kind of processor. 我想检查一下我使用的操作系统类型和处理器类型。 this should be check on run time. 这应该在运行时检查。 i tried using 我试过用

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

and

System.OperatingSystem osInfo2 = System.Environment.OSVersion;
Console.WriteLine(osInfo2.ToString());

but it's just the enviroment that VS is running on. 但这只是VS正在运行的环境。
I was told to use WMI to check it but i can't find out how. 有人告诉我使用WMI检查它,但我不知道如何。 can someone help me with that? 有人可以帮助我吗?

Retrieving OS info: 检索操作系统信息:

var wmi =
    new ManagementObjectSearcher( "select * from Win32_OperatingSystem" )
    .Get()
    .Cast<ManagementObject>()
    .First();

OS.Name = ((string)wmi["Caption"]).Trim();
OS.Version = (string)wmi["Version"];
OS.MaxProcessCount = (uint)wmi["MaxNumberOfProcesses"];
OS.MaxProcessRAM = (ulong)wmi["MaxProcessMemorySize"];
OS.Architecture = (string)wmi["OSArchitecture"];
OS.SerialNumber = (string)wmi["SerialNumber"];
OS.Build = ((string)wmi["BuildNumber"]).ToUint();

Retrieving CPU info: 检索CPU信息:

var cpu =
    new ManagementObjectSearcher( "select * from Win32_Processor" )
    .Get()
    .Cast<ManagementObject>()
    .First();

CPU.ID = (string)cpu["ProcessorId"];
CPU.Socket = (string)cpu["SocketDesignation"];
CPU.Name = (string)cpu["Name"];
CPU.Description = (string)cpu["Caption"];
CPU.AddressWidth = (ushort)cpu["AddressWidth"];
CPU.DataWidth = (ushort)cpu["DataWidth"];
CPU.Architecture = (CPU.CpuArchitecture)(ushort)cpu["Architecture"];
CPU.SpeedMHz = (uint)cpu["MaxClockSpeed"];
CPU.BusSpeedMHz = (uint)cpu["ExtClock"];
CPU.L2Cache = (uint)cpu["L2CacheSize"] * (ulong)1024;
CPU.L3Cache = (uint)cpu["L3CacheSize"] * (ulong)1024;
CPU.Cores = (uint)cpu["NumberOfCores"];
CPU.Threads = (uint)cpu["NumberOfLogicalProcessors"];

CPU.Name =
   CPU.Name
   .Replace( "(TM)", "™" )
   .Replace( "(tm)", "™" )
   .Replace( "(R)", "®" )
   .Replace( "(r)", "®" )
   .Replace( "(C)", "©" )
   .Replace( "(c)", "©" )
   .Replace( "    ", " " )
   .Replace( "  ", " " );

是的WMI是执行此类操作的最佳方式您可以使用它来检索操作系统信息:

ManagementObjectSearcher objMOS = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM  Win32_OperatingSystem");

To determine the operating system use this code: 要确定操作系统,请使用以下代码:

string OPSystemVersion = Environment.OSVersion.ToString();

To determine the CPU name and type first add System.Management reference to your project, then you can use this code: 要确定CPU名称和类型,首先添加对项目的System.Management引用,然后您可以使用以下代码:

public static string SendBackProcessorName()
        {
            ManagementObjectSearcher mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
            string Procname = null;

            foreach (ManagementObject moProcessor in mosProcessor.Get())
            {
                if (moProcessor["name"] != null)
                {
                    Procname = moProcessor["name"].ToString();

                }

            }

            return Procname;
        }

Look at the ManagementClass class: http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx 查看ManagementClass类: http//msdn.microsoft.com/en-us/library/system.management.managementclass.aspx

var mgmt = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject mgmtObj in mgmt.GetInstances())
{                
    // Just get first value.
    return mgmtObj[info.Information].ToString().Trim();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM