用C#获取CPU编号、硬盘编号等系统有关环境、属性

简介:
如果利用C#获取系统有关环境和属性,这个也是在网上问得比较多的问题,不过大部分只有提问没有回答,最近正好想做有关方面的东西,整理了一下,提供给大家,希望能给大家提供参考意见:

首先需要定义几个结构(struct) ,便于DllImport作为返回参数调用。以下是代码:
CpuInfo.cs
using System; 
using System.Configuration; 
using System.Runtime.InteropServices; 
 
/**//** 
 * LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序 
 * 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。 
 * LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序 
 * LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。 
 */
 
    
/** //// <summary> 
/// 定义CPU的信息结构 
/// </summary> 
[StructLayout(LayoutKind.Sequential)]    
public  struct CpuInfo 
...{ 
        /** //// <summary> 
         /// OEM ID 
         /// </summary> 
         public  uint dwOemId; 
        /** //// <summary> 
         /// 页面大小 
         /// </summary> 
         public  uint dwPageSize; 
         public  uint lpMinimumApplicationAddress; 
         public  uint lpMaximumApplicationAddress; 
         public  uint dwActiveProcessorMask; 
        /** //// <summary> 
         /// CPU个数 
         /// </summary> 
         public  uint dwNumberOfProcessors; 
        /** //// <summary> 
         /// CPU类型 
         /// </summary> 
         public  uint dwProcessorType; 
         public  uint dwAllocationGranularity; 
        /** //// <summary> 
         /// CPU等级 
         /// </summary> 
         public  uint dwProcessorLevel; 
         public  uint dwProcessorRevision;    
}


MemoryInfo.cs
using System; 
using System.Configuration; 
using System.Runtime.InteropServices; 
 
/**//** 
 * LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序 
 * 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。 
 * LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序 
 * LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。 
 */
 
/** //// <summary> 
/// 定义内存的信息结构 
/// </summary> 
[StructLayout(LayoutKind.Sequential)] 
public  struct MemoryInfo 
...{ 
        /** //// <summary> 
         ///    
         /// </summary> 
         public  uint dwLength; 
        /** //// <summary> 
         /// 已经使用的内存 
         /// </summary> 
         public  uint dwMemoryLoad; 
        /** //// <summary> 
         /// 总物理内存大小 
         /// </summary> 
         public  uint dwTotalPhys; 
        /** //// <summary> 
         /// 可用物理内存大小 
         /// </summary> 
         public  uint dwAvailPhys; 
        /** //// <summary> 
         /// 交换文件总大小 
         /// </summary> 
         public  uint dwTotalPageFile; 
        /** //// <summary> 
         /// 可用交换文件大小 
         /// </summary> 
         public  uint dwAvailPageFile; 
        /** //// <summary> 
         /// 总虚拟内存大小 
         /// </summary> 
         public  uint dwTotalVirtual; 
        /** //// <summary> 
         /// 可用虚拟内存大小 
         /// </summary> 
         public  uint dwAvailVirtual; 
}


SystemTimeInfo.cs
using System; 
using System.Configuration; 
using System.Runtime.InteropServices; 
 
/**//** 
 * LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序 
 * 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。 
 * LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序 
 * LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。 
 */
 
/** //// <summary> 
/// 定义系统时间的信息结构 
/// </summary> 
[StructLayout(LayoutKind.Sequential)]    
public  struct SystemTimeInfo 
...{ 
        /** //// <summary> 
         /// 年 
         /// </summary> 
         public  ushort wYear; 
        /** //// <summary> 
         /// 月 
         /// </summary> 
         public  ushort wMonth; 
        /** //// <summary> 
         /// 星期 
         /// </summary> 
         public  ushort wDayOfWeek; 
        /** //// <summary> 
         /// 天 
         /// </summary> 
         public  ushort wDay; 
        /** //// <summary> 
         /// 小时 
         /// </summary> 
         public  ushort wHour; 
        /** //// <summary> 
         /// 分钟 
         /// </summary> 
         public  ushort wMinute; 
        /** //// <summary> 
         /// 秒 
         /// </summary> 
         public  ushort wSecond; 
        /** //// <summary> 
         /// 毫秒 
         /// </summary> 
         public  ushort wMilliseconds; 
}


另外还定义了一个调用类SystemInfo.cs,代码如下:
using System; 
using System.Configuration; 
using System.Runtime.InteropServices; 
using System.Management; 
using System.Text; 
 
/** //// <summary> 
/// SystemInfo 的摘要说明 
/// </summary> 
public  class SystemInfo 
...{ 
         private  const  int CHAR_COUNT = 128; 
         public SystemInfo() 
        ...{ 
                 
        } 
        [DllImport( "kernel32")] 
         private  static  extern  void GetWindowsDirectory(StringBuilder WinDir,  int count); 
 
        [DllImport( "kernel32")] 
         private  static  extern  void GetSystemDirectory(StringBuilder SysDir,  int count); 
 
        [DllImport( "kernel32")] 
         private  static  extern  void GetSystemInfo( ref CpuInfo cpuInfo); 
 
        [DllImport( "kernel32")] 
         private  static  extern  void GlobalMemoryStatus( ref MemoryInfo memInfo); 
 
        [DllImport( "kernel32")] 
         private  static  extern  void GetSystemTime( ref SystemTimeInfo sysInfo); 
 
        /** //// <summary> 
         /// 查询CPU编号 
         /// </summary> 
         /// <returns></returns> 
         public  string GetCpuId() 
        ...{ 
                ManagementClass mClass =  new ManagementClass( "Win32_Processor"); 
                ManagementObjectCollection moc = mClass.GetInstances(); 
                 string cpuId= null
                 foreach (ManagementObject mo  in moc) 
                ...{ 
                        cpuId = mo.Properties[ "ProcessorId"].Value.ToString(); 
                         break
                } 
                 return cpuId; 
        } 
 
        /** //// <summary> 
         /// 查询硬盘编号 
         /// </summary> 
         /// <returns></returns> 
         public  string GetMainHardDiskId() 
        ...{ 
                ManagementObjectSearcher searcher =  new ManagementObjectSearcher( "SELECT * FROM Win32_PhysicalMedia"); 
                String hardDiskID= null
                 foreach (ManagementObject mo  in searcher.Get()) 
                ...{ 
                        hardDiskID = mo[ "SerialNumber"].ToString().Trim(); 
                         break
                } 
                 return hardDiskID;    
        } 
 
        /** //// <summary> 
         /// 获取Windows目录 
         /// </summary> 
         /// <returns></returns> 
         public  string GetWinDirectory() 
        ...{ 
                StringBuilder sBuilder =  new StringBuilder(CHAR_COUNT); 
                GetWindowsDirectory(sBuilder, CHAR_COUNT); 
                 return sBuilder.ToString(); 
        } 
 
        /** //// <summary> 
         /// 获取系统目录 
         /// </summary> 
         /// <returns></returns> 
         public  string GetSysDirectory() 
        ...{ 
                StringBuilder sBuilder =  new StringBuilder(CHAR_COUNT); 
                GetSystemDirectory(sBuilder, CHAR_COUNT); 
                 return sBuilder.ToString(); 
        } 
 
     /** //// <summary> 
      /// 获取CPU信息 
      /// </summary> 
      /// <returns></returns> 
         public CpuInfo GetCpuInfo() 
        ...{ 
                CpuInfo cpuInfo =  new CpuInfo(); 
                GetSystemInfo( ref cpuInfo); 
                 return cpuInfo; 
        } 
 
        /** //// <summary> 
         /// 获取系统内存信息 
         /// </summary> 
         /// <returns></returns> 
         public MemoryInfo GetMemoryInfo() 
        ...{ 
                MemoryInfo memoryInfo =  new MemoryInfo(); 
                GlobalMemoryStatus( ref memoryInfo); 
                 return memoryInfo; 
        } 
 
        /** //// <summary> 
         /// 获取系统时间信息 
         /// </summary> 
         /// <returns></returns> 
         public SystemTimeInfo GetSystemTimeInfo() 
        ...{ 
                SystemTimeInfo systemTimeInfo =  new SystemTimeInfo(); 
                GetSystemTime( ref systemTimeInfo); 
                 return systemTimeInfo; 
        } 
 
        /** //// <summary> 
         /// 获取系统名称 
         /// </summary> 
         /// <returns></returns> 
         public  string GetOperationSystemInName() 
        ...{ 
                OperatingSystem os = System.Environment.OSVersion; 
                 string osName =  "UNKNOWN"
                 switch (os.Platform) 
                ...{ 
                         case PlatformID.Win32Windows: 
                                 switch (os.Version.Minor) 
                                ...{ 
                                         case 0: osName =  "Windows 95"break
                                         case 10: osName =  "Windows 98"break
                                         case 90: osName =  "Windows ME"break
                                } 
                                 break
                         case PlatformID.Win32NT: 
                                 switch (os.Version.Major) 
                                ...{ 
                                         case 3: osName =  "Windws NT 3.51"break
                                         case 4: osName =  "Windows NT 4"break
                                         case 5:  if (os.Version.Minor == 0) 
                                                ...{ 
                                                        osName =  "Windows 2000";    
                                                } 
                                                 else  if (os.Version.Minor == 1) 
                                                ...{ 
                                                        osName =  "Windows XP";    
                                                } 
                                                 else  if (os.Version.Minor == 2) 
                                                ...{ 
                                                        osName =  "Windows Server 2003";    
                                                } 
                                                 break
                                         case 6: osName =  "Longhorn"break
                                } 
                                 break
                } 
                 return String.Format( "{0},{1}", osName, os.Version.ToString()); 
        } 
}


以下是调用实例,为了简单,我在一个aspx页面中输出,不过这个程序可以在WinForm中调用:
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Collections.Specialized; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Runtime.InteropServices; 
 
public partial  class Index : System.Web.UI.Page 
...{ 
         protected  void Page_Load( object sender, EventArgs e) 
        ...{ 
                 if (!Page.IsPostBack) 
                ...{ 
                        SystemInfo systemInfo =  new SystemInfo(); 
                         Response.Write( "操作系统:" + systemInfo.GetOperationSystemInName() +  "<br>"); 
                        Response.Write( "CPU编号:"+systemInfo.GetCpuId() +  "<br>"); 
                        Response.Write( "硬盘编号:"+systemInfo.GetMainHardDiskId() +  "<br>"); 
                        Response.Write( "Windows目录所在位置:" + systemInfo.GetSysDirectory() +  "<br>"); 
                        Response.Write( "系统目录所在位置:" + systemInfo.GetWinDirectory() +  "<br>"); 
                        MemoryInfo memoryInfo = systemInfo.GetMemoryInfo(); 
                        CpuInfo cpuInfo = systemInfo.GetCpuInfo(); 
                        Response.Write( "dwActiveProcessorMask" + cpuInfo.dwActiveProcessorMask +  "<br>"); 
                        Response.Write( "dwAllocationGranularity" + cpuInfo.dwAllocationGranularity +  "<br>"); 
                        Response.Write( "CPU个数:" + cpuInfo.dwNumberOfProcessors +  "<br>"); 
                        Response.Write( "OEM ID:" + cpuInfo.dwOemId +  "<br>"); 
                        Response.Write( "页面大小" + cpuInfo.dwPageSize +  "<br>"); 
                        Response.Write( "CPU等级" + cpuInfo.dwProcessorLevel +  "<br>"); 
                        Response.Write( "dwProcessorRevision" + cpuInfo.dwProcessorRevision +  "<br>"); 
                        Response.Write( "CPU类型" + cpuInfo.dwProcessorType +  "<br>"); 
                        Response.Write( "lpMaximumApplicationAddress" + cpuInfo.lpMaximumApplicationAddress +  "<br>"); 
                        Response.Write( "lpMinimumApplicationAddress" + cpuInfo.lpMinimumApplicationAddress +  "<br>"); 
                        Response.Write( "CPU类型:" + cpuInfo.dwProcessorType +  "<br>"); 
                        Response.Write( "可用交换文件大小:" + memoryInfo.dwAvailPageFile +  "<br>"); 
                        Response.Write( "可用物理内存大小:" + memoryInfo.dwAvailPhys +  "<br>"); 
                        Response.Write( "可用虚拟内存大小" + memoryInfo.dwAvailVirtual +  "<br>"); 
                        Response.Write( "操作系统位数:" + memoryInfo.dwLength +  "<br>"); 
                        Response.Write( "已经使用内存大小:" + memoryInfo.dwMemoryLoad +  "<br>"); 
                        Response.Write( "交换文件总大小:" + memoryInfo.dwTotalPageFile +  "<br>"); 
                        Response.Write( "总物理内存大小:" + memoryInfo.dwTotalPhys +  "<br>"); 
                        Response.Write( "总虚拟内存大小:" + memoryInfo.dwTotalVirtual +  "<br>"); 
                } 
        } 


说明:前台aspx页面没有任何控件。

















本文转自周金桥51CTO博客,原文链接: http://blog.51cto.com/zhoufoxcn/167082 ,如需转载请自行联系原作者

相关文章
|
3月前
|
缓存 监控 Linux
在Linux中,如何看当前系统有几颗物理CPU和每颗CPU的核数?
在Linux中,如何看当前系统有几颗物理CPU和每颗CPU的核数?
|
30天前
|
存储 开发框架 .NET
C#语言如何搭建分布式文件存储系统
C#语言如何搭建分布式文件存储系统
63 2
|
19天前
|
运维 JavaScript Linux
容器内的Nodejs应用如何获取宿主机的基础信息-系统、内存、cpu、启动时间,以及一个df -h的坑
本文介绍了如何在Docker容器内的Node.js应用中获取宿主机的基础信息,包括系统信息、内存使用情况、磁盘空间和启动时间等。核心思路是将宿主机的根目录挂载到容器,但需注意权限和安全问题。文章还提到了使用`df -P`替代`df -h`以获得一致性输出,避免解析错误。
|
30天前
|
存储 分布式计算 监控
C# 创建一个分布式文件存储系统需要怎么设计??
C# 创建一个分布式文件存储系统需要怎么设计??
29 0
|
30天前
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
39 0
|
3月前
|
监控 Linux
性能分析之 Linux 系统中 ps&top 中 CPU 百分比不一致?
【8月更文挑战第18天】性能分析之 Linux 系统中 ps&top 中 CPU 百分比不一致?
147 4
|
3月前
|
Linux Python
在Linux中,如何查找系统中占用CPU最高的进程?
在Linux中,如何查找系统中占用CPU最高的进程?
|
3月前
|
缓存 Kubernetes 数据中心
在Docker中,如何控制容器占用系统资源(CPU,内存)的份额?
在Docker中,如何控制容器占用系统资源(CPU,内存)的份额?
|
3月前
|
数据安全/隐私保护 异构计算 Windows
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
|
3月前
|
缓存 Ubuntu Linux
在Linux中,如何检查系统的CPU和内存使用情况?
在Linux中,如何检查系统的CPU和内存使用情况?