用Visual C#获取系统信息四法

    技术2026-01-19  5

    用Visual C#获取系统信息四法 2007-12-07 13:30 第一种方法:用SystemInformation 类

    SystemInformation 提供静态方法和属性,它们可用于获取诸如 Windows 显示元素大小、操作系统设置、网络可用性和系统中所安装硬件的性能等信息,这种方法侧重于操作系统(这里特指Windows)的一些设置和状态。

        第二种方法 : 用Environment 类

    通过这种方法可检索的系统信息包括命令行参数、环境变量设置、调用堆栈的内容、上次系统引导以来的时间,以及公共语言运行库的版本等等,测重于使用这个类的应用程序所处的环境和状态,也有比较多的动态信息,比如开机启动后的毫秒数等等。

    第三种方法 用RegistryKey 类

    这种方法方法读取的也只能是静态的设置,不过比上面两种方法更加接近操作系统,因为值是直接从注册表中读取出来的,有好处也有不好的地方,好处就是基本上可以获得任何设置。不好的地方是不够便利,毕竟,检索注册表的键值不如直接调用方法来得方便。

    第四种方法 用API 函数

    这种方法严格说来不是用C#来实现对系统信息的读取,因为它实际调用的是系统API。这种方法有更加有意义的地方,就在其对API功能的调用,因而我们可以做出更多的事情。这种方法也可以在Java中依样画胡芦地用到。

     

    下面讲一下程序结构:

    //RegistryKey 类所需要的包:

    using Microsoft.Win32; //DllImport方法所需要的包: using System.Runtime.InteropServices; //StringBuilder所需要的包: using System.Text; //声明API函数        [DllImport("kernel32")]        public static extern void GetWindowsDirectory(StringBuilder WinDir,int count);        [DllImport("kernel32")]        public static extern void GetSystemDirectory(StringBuilder SysDir,int count);        [DllImport("kernel32")]        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);        [DllImport("kernel32")]        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);        [DllImport("kernel32")]        public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);        //定义以下各结构        //定义CPU的信息结构        [StructLayout(LayoutKind.Sequential)]            public struct CPU_INFO        {            public uint dwOemId;            public uint dwPageSize;            public uint lpMinimumApplicationAddress;            public uint lpMaximumApplicationAddress;            public uint dwActiveProcessorMask;            public uint dwNumberOfProcessors;            public uint dwProcessorType;            public uint dwAllocationGranularity;            public uint dwProcessorLevel;            public uint dwProcessorRevision;        }       //定义内存的信息结构        [StructLayout(LayoutKind.Sequential)]            public struct MEMORY_INFO        {            public uint dwLength;            public uint dwMemoryLoad;            public uint dwTotalPhys;            public uint dwAvailPhys;            public uint dwTotalPageFile;            public uint dwAvailPageFile;            public uint dwTotalVirtual;            public uint dwAvailVirtual;        }        //定义系统时间的信息结构        [StructLayout(LayoutKind.Sequential)]            public struct SYSTEMTIME_INFO        {            public ushort wYear;            public ushort wMonth;            public ushort wDayOfWeek;            public ushort wDay;            public ushort wHour;            public ushort wMinute;            public ushort wSecond;            public ushort wMilliseconds;        } private void initSysInfoData()        {            //获取操作系统设置            lstSysInfo.Items.Add("计算机名 : " + SystemInformation.ComputerName );            lstSysInfo.Items.Add("是否已连接网络 : " + SystemInformation.Network );            lstSysInfo.Items.Add("用户域 : " + SystemInformation.UserDomainName );                       lstSysInfo.Items.Add("当前线程用户名   : " + SystemInformation.UserName   );                   lstSysInfo.Items.Add("启动方式 : " + SystemInformation.BootMode );            lstSysInfo.Items.Add("菜单的字体 : " + SystemInformation.MenuFont );            lstSysInfo.Items.Add("显示器的数目 : " + SystemInformation.MonitorCount );            lstSysInfo.Items.Add("鼠标已安装 : " + SystemInformation.MousePresent );            lstSysInfo.Items.Add("鼠标按钮数    : " + SystemInformation.MouseButtons);               lstSysInfo.Items.Add("是否交互模式    : " + SystemInformation.UserInteractive    );            lstSysInfo.Items.Add("屏幕界限: " + SystemInformation.VirtualScreen );        }        public void initEnvData()        {                    //获取程序运行的相关信息.            lstEnv.Items.Add("命令行:"+ Environment.CommandLine);             lstEnv.Items.Add("命令行参数:"+ String.Join(", ",Environment.GetCommandLineArgs()));            lstEnv.Items.Add("当前目录:"+ Environment.CurrentDirectory);            lstEnv.Items.Add("操作系统版本:"+ Environment.OSVersion);            lstEnv.Items.Add("系统目录:"+ Environment.SystemDirectory);             lstEnv.Items.Add("堆栈信息:"+ Environment.StackTrace);             lstEnv.Items.Add("用户域:"+ Environment.UserDomainName);            lstEnv.Items.Add("CLR版本:"+ Environment.Version);            lstEnv.Items.Add("系统启动后经过的毫秒:"+ Environment.TickCount);             lstEnv.Items.Add("进程上下文的物理内存量:"+ Environment.WorkingSet);               String[] drives = Environment.GetLogicalDrives();            lstEnv.Items.Add("本机磁盘驱动器: "+ String.Join(", ", drives));                // 获取本机所有环境变量            IDictionary    environmentVariables = Environment.GetEnvironmentVariables();            foreach (DictionaryEntry de in environmentVariables)            {               lstEnv.Items.Add(de.Key+"="+de.Value);            }                       }        public void initRegKeyData()         {            //通过注册表获取信息            RegistryKey Rkey = Registry.LocalMachine;            Rkey = Rkey.OpenSubKey("HARDWARE//DESCRIPTION//System//CentralProcessor//0");            lstRegkey.Items.Add("处理器信息:"+Rkey.GetValue("ProcessorNameString").ToString().Trim());                       Rkey=Registry.LocalMachine;            Rkey = Rkey.OpenSubKey("SOFTWARE//Microsoft//Windows NT//CurrentVersion//NetworkCards//1");            lstRegkey.Items.Add("网卡信息:"+(String)Rkey.GetValue("Description"));         }        public void initAPIData()        {           //调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径            const int nChars = 128;            StringBuilder Buff = new StringBuilder(nChars);            GetWindowsDirectory(Buff,nChars);            lstAPI.Items.Add("Windows路径:"+Buff.ToString());            GetSystemDirectory(Buff,nChars);            lstAPI.Items.Add("系统路径:"+Buff.ToString());            //调用GetSystemInfo函数获取CPU的相关信息            CPU_INFO CpuInfo;            CpuInfo = new CPU_INFO();            GetSystemInfo(ref CpuInfo);            //CPU信息的读取是错误的,我本只有一个CPU,读成了两个            lstAPI.Items.Add("本计算机中有"+CpuInfo.dwNumberOfProcessors.ToString()+"个CPU");            lstAPI.Items.Add("CPU的类型为"+CpuInfo.dwProcessorType.ToString());            lstAPI.Items.Add("CPU等级为"+CpuInfo.dwProcessorLevel.ToString());            lstAPI.Items.Add("CPU的OEM ID为"+CpuInfo.dwOemId.ToString());            lstAPI.Items.Add("CPU中的页面大小为"+CpuInfo.dwPageSize.ToString());            //调用GlobalMemoryStatus函数获取内存的相关信息            MEMORY_INFO MemInfo;            MemInfo = new MEMORY_INFO();            GlobalMemoryStatus(ref MemInfo);            lstAPI.Items.Add( MemInfo.dwMemoryLoad.ToString()+"%的内存正在使用");            lstAPI.Items.Add("物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节");            lstAPI.Items.Add("可使用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节");            lstAPI.Items.Add( "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString()+"字节");            lstAPI.Items.Add( "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString()+"字节");            lstAPI.Items.Add( "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节");            lstAPI.Items.Add( "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString()+"字节");             //调用GetSystemTime函数获取系统时间信息            SYSTEMTIME_INFO StInfo;            StInfo = new SYSTEMTIME_INFO();            GetSystemTime(ref StInfo);            lstAPI.Items.Add(StInfo.wYear.ToString()+"年"+StInfo.wMonth.ToString()+"月"+StInfo.wDay.ToString()+"日");            lstAPI.Items.Add((StInfo.wHour+8).ToString()+"点"+StInfo.wMinute.ToString()+"分"+StInfo.wSecond.ToString()+"秒");

           }

    最新回复(0)