C#中,获取系统信息全解

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Management;
//注意:在Visual Studio中,进行了using System.Management
//下面还是会调用不到,需要在引用(reference)中添加这个dll
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
 
namespace util
{
     class 系统信息类
     {
         [DllImport( "kernel32" )]
         public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
         [DllImport( "kernel32" )]
         public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
         [DllImport( "kernel32" )]
         private static extern void GlobalMemoryStatus(ref 内存信息结构体 memibfo);
         [DllImport( "kernel32" )]
         public static extern void GetSystemInfo(ref cpu信息结构体 cpuinfo);
         [DllImport( "kernel32" )]
         public static extern void GetSystemTime(ref 系统时间信息结构体 stinfo);
         [DllImport( "Iphlpapi.dll" )]
         public static extern int SendARP(Int32 DestIP,Int32 SrcIP, ref Int64 MacAddr,ref Int32 PhyAddrLen);
         [DllImport( "Ws2_32.dll" )]
         public static extern Int32 inet_addr(string ipaddr);
 
         public struct 内存信息结构体
         {
             public uint 长度;
             public uint 内存使用率;
             public uint 总物理内存; //此处全是以字节为单位
             public uint 可用物理内存;
             public uint 交换文件总大小;
             public uint 可用交换文件大小;
             public uint 总虚拟内存;
             public uint 可用虚拟内存大小;
         }
 
         [StructLayout(LayoutKind.Sequential)]
         public struct cpu信息结构体
         {
             public uint cpu的OemId;
             public uint cpu页面大小;
             public uint lpMinimumApplicationAddress;
             public uint lpMaximumApplicationAddress;
             public uint dwActiveProcessorMask;
             public uint cpu个数;
             public uint cpu类别;
             public uint dwAllocationGranularity;
             public uint cpu等级;
             public uint cpu修正;
         }
         [StructLayout(LayoutKind.Sequential)]
         public struct 系统时间信息结构体
         {
             public ushort wYear;
             public ushort wMonth;
             public ushort wDayOfWeek;
             public ushort wDay;
             public ushort wHour;
             public ushort wMinute;
             public ushort wSecond;
             public ushort wMilliseconds;
         }
         public static string 内存利用率函数()
         {
             内存信息结构体 memInfor = new 内存信息结构体();
             GlobalMemoryStatus(ref memInfor);
             return memInfor.内存使用率.ToString( "0.0" );
         }
         public static string 系统路径()
         {
             const int nChars = 128 ;
             StringBuilder Buff = new StringBuilder(nChars);
             GetSystemDirectory(Buff, nChars);
             return Buff.ToString();
         }
         public static string window路径()
         {
             const int nChars = 128 ;
             StringBuilder Buff = new StringBuilder(nChars);
             GetWindowsDirectory(Buff, nChars);
             return Buff.ToString();
         }
         public static 内存信息结构体 内存自定义函数()
         {
             内存信息结构体 memInfor = new 内存信息结构体();
             GlobalMemoryStatus(ref memInfor);
             return memInfor;
         }
         public static cpu信息结构体 cpu自定义函数()
         {
             cpu信息结构体 memInfor = new cpu信息结构体();
             GetSystemInfo(ref memInfor);
             return memInfor;
         }
         public static 系统时间信息结构体 系统时间自定义函数()
         {
             系统时间信息结构体 memInfor = new 系统时间信息结构体();
             GetSystemTime(ref memInfor);
             return memInfor;
         }
 
         public static string cpu序列号函数()
             {
                 try
                 {
                     string cpuInfo = "" ; //cpu序列号
                     ManagementClass mc = new ManagementClass( "Win32_Processor" );
                     ManagementObjectCollection moc = mc.GetInstances();
                     foreach (ManagementObject mo in moc)
                     {
                       cpuInfo = mo.Properties[ "ProcessorId" ].Value.ToString();
                     }
                     moc = null ;
                     mc = null ;
                     return cpuInfo;
                 }
                 catch
                 {
                    return "unknow" ;
                 }
             }
         public static string 获得mac地址函数()
             {
                 string mac = "" ;
                 ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration" );
                 ManagementObjectCollection moc = mc.GetInstances();
                 foreach (ManagementObject mo in moc)
                 {
                     if ((bool)mo[ "IPEnabled" ] == true )
                     {
                         mac = mo[ "MacAddress" ].ToString();
                     }
                     mo.Dispose();
                 }
                 return mac;
             }
         public static string 获得远程目标mac地址函数(string ip地址)
           {
               StringBuilder mac = new StringBuilder();
               try
               {
                   Int32 remote = inet_addr(ip地址);
                   Int64 macinfo = new Int64();
                   Int32 length = 6 ;
                   SendARP(remote, 0 ,ref macinfo,ref length);
                   string temp = Convert.ToString(macinfo, 16 ).PadLeft( 12 , '0' ).ToUpper();
                   int x = 12 ;
                   for ( int i = 0 ; i < 6 ; i++)
                   {
                       if (i == 5 )
                           mac.Append(temp.Substring(x - 2 , 2 ));
                       else
                           mac.Append(temp.Substring(x - 2 , 2 ) + "-" );
                       x -= 2 ;
                   }
                   return mac.ToString();
               }
               catch
               {
                   return mac.ToString();
               }
           }
         public static string 获得ip地址函数()
             {
                 try
                 {
                     string st = "" ;
                     ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration" );
                     ManagementObjectCollection moc = mc.GetInstances();
                     foreach (ManagementObject mo in moc)
                     {
                         if ((bool)mo[ "IPEnabled" ] == true )
                         {
                             //st=mo["IpAddress"].ToString();
                             System.Array ar;
                             ar = (System.Array)(mo.Properties[ "IpAddress" ].Value);
                             st = ar.GetValue( 0 ).ToString();
                             break ;
                         }
                     }
                     moc = null ;
                     mc = null ;
                     return st;
                 }
                 catch
                 {
                     return "unknow" ;
                 }
             }
         public static string 获取硬盘ID函数()
             {
                 try
                 {
                     String HDid = "" ;
                     ManagementClass mc = new ManagementClass( "Win32_DiskDrive" );
                     ManagementObjectCollection moc = mc.GetInstances();
                     foreach (ManagementObject mo in moc)
                     {
                         HDid = (string)mo.Properties[ "Model" ].Value;
                     }
                     moc = null ;
                     mc = null ;
                     return HDid;
                 }
                 catch
                 {
                     return "unknow" ;
                 }
             }
         public static string 获得系统登陆用户名函数()
             {
                 try
                 {
                     string st = "" ;
                     ManagementClass mc = new ManagementClass( "Win32_ComputerSystem" );
                     ManagementObjectCollection moc = mc.GetInstances();
                     foreach (ManagementObject mo in moc)
                     {
                         st = mo[ "UserName" ].ToString();
                     }
                     moc = null ;
                     mc = null ;
                     return st;
                 }
                 catch
                 {
                     return "unknow" ;
                 }
             }
         public static string 获得电脑类型函数()
             {
                 try
                 {
                     string st = "" ;
                     ManagementClass mc = new ManagementClass( "Win32_ComputerSystem" );
                     ManagementObjectCollection moc = mc.GetInstances();
                     foreach (ManagementObject mo in moc)
                     {
                         st = mo[ "SystemType" ].ToString();
                     }
                     moc = null ;
                     mc = null ;
                     return st;
                 }
                 catch
                 {
                     return "unknow" ;
                 }
             }
         public static string 获得物理总内存函数()
           {
               try
               {
                   string st = "" ;
                   ManagementClass mc = new ManagementClass( "Win32_ComputerSystem" );
                   ManagementObjectCollection moc = mc.GetInstances();
                   foreach (ManagementObject mo in moc)
                   {
                       st = mo[ "TotalPhysicalMemory" ].ToString();
                   }
                   moc = null ;
                   mc = null ;
                   return st;
               }
               catch
               {
                   return "unknow" ;
               }
           }
         public static string 获得电脑名称函数()
          {
              try
              {
                  return System.Environment.GetEnvironmentVariable( "ComputerName" );
              }
              catch
              {
                  return "unknow" ;
              }
          }
         public static float 性能显示状况 2 (string CategoryName, string CounterName)
         {
             PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName);
             Thread.Sleep( 500 ); //waitfor1second
             float xingneng = pc.NextValue();
             return xingneng;
         }
 
         public static float 性能显示状况 3 (string CategoryName, string CounterName, string InstanceName)
         {
             PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
             Thread.Sleep( 500 ); //waitfor1second
             float xingneng = pc.NextValue();
             return xingneng;
         }
         public static string os版本信息函数()
         {
             System.OperatingSystem 版本信息 = System.Environment.OSVersion;
             switch (版本信息.Platform)
             {
                 case System.PlatformID.Win32Windows:
                     switch (版本信息.Version.Minor)
                     {
                         case 0 :
                             return "Windows 95" ;
                             break ;
                         case 10 :
                             if (版本信息.Version.Revision.ToString() == "2222A" )
                                 return "Windows 98 Second Edition" ;
                             else
                                 return "Windows 98" ;
                             break ;
                         case 90 :
                             return "Windows Me" ;
                             break ;
                     }
                     break ;
                 case System.PlatformID.Win32NT:
                     switch (版本信息.Version.Major)
                     {
                         case 3 :
                             return "Windows NT 3.51" ;
                             break ;
                         case 4 :
                             return "Windows NT 4.0" ;
                             break ;
                         case 5 :
                             if (版本信息.Version.Minor == 0 )
                                 return "Windows 2000" ;
                             else
                                 return "Windows XP" ;
                             break ;
                         case 6 :
                             return "Windows 8" ;
                             break ;
                     }
                     break ;
             }
             return "发现失败" ;
 
         }
     }
 
}


本文转载自:https://www.2cto.com/kf/201708/668145.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值