C#获取用户系统信息的程序
在这个程序当中,主要通过下面两个类获取用户系统的信息:
System.Environment
System.Managememnt
应用程序包括三个tab菜单,分别是:
System
CPU
Local drives
在 System 菜单中,用 System.Environment 类来获取系统信息 :
[csharp]
private string SystemInformation()
{
StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
try
{
StringBuilder1.AppendFormat("Operation System: {0}\n", Environment.OSVersion);
if (Environment.Is64BitOperatingSystem)
StringBuilder1.AppendFormat("\t\t 64 Bit Operating System\n");
else
StringBuilder1.AppendFormat("\t\t 32 Bit Operating System\n");
StringBuilder1.AppendFormat("SystemDirectory: {0}\n", Environment.SystemDirectory);
StringBuilder1.AppendFormat("ProcessorCount: {0}\n", Environment.ProcessorCount);
StringBuilder1.AppendFormat("UserDomainName: {0}\n", Environment.UserDomainName);
StringBuilder1.AppendFormat("UserName: {0}\n", Environment.UserName);
//Drives
StringBuilder1.AppendFormat("LogicalDrives:\n");
foreach (System.IO.DriveInfo DriveInfo1 in System.IO.DriveInfo.GetDrives())
{
try
{
StringBuilder1.AppendFormat("\t Drive: {0}\n\t\t VolumeLabel: " +
"{1}\n\t\t DriveType: {2}\n\t\t DriveFormat: {3}\n\t\t " +
"TotalSize: {4}\n\t\t AvailableFreeSpace: {5}\n",
DriveInfo1.Name, DriveInfo1.VolumeLabel, DriveInfo1.DriveType,
DriveInfo1.DriveFormat, DriveInfo1.TotalSize, DriveInfo1.AvailableFreeSpace);
}
catch
{
}
}
StringBuilder1.AppendFormat("SystemPageSize: {0}\n", Environment.SystemPageSize);
StringBuilder1.AppendFormat("Version: {0}", Environment.Version);
}
catch
{
}
return StringBuilder1.ToString();
}
在 CPU 和 Local drive 选项卡, 类 System.Management 用来收集信息,但是在使用该类之前需要加入引用. 加入System.Management引用的步骤如下:
打开项目解决方案.
右键单击引用,选择添加引用.
单击.NET 选项卡.
选择System.Management.
单击确定按钮.
现在可以用该类写下面的函数:
[csharp]
private string DeviceInformation(string stringIn)
{
StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
ManagementClass ManagementClass1 = new ManagementClass(stringIn);
//Create a ManagementObjectCollection to loop through
ManagementObjectCollection ManagemenobjCol = ManagementClass1.GetInstances();
//Get the properties in the class
PropertyDataCollection properties = ManagementClass1.Properties;
foreach (ManagementObject obj in ManagemenobjCol)
{
foreach (PropertyData property in properties)
{
try
{
StringBuilder1.AppendLine(property.Name + ": " +
obj.Properties[property.Name].Value.ToString());
}
catch
{
//Add codes to manage more informations
}
}
StringBuilder1.AppendLine();
}
return StringBuilder1.ToString();
}
这个函数有一个参数,该参数的值可以是Win32_Processor 或Win32_LogicalDisk。用第一次参数来获取CPU的信息,用第二参数来获取硬盘驱动器的信息。最后再Window_Loaded函数中调用如下语句:
[csharp]
//System Information
textBox1.Text = SystemInformation();
//CPU Information
textBox2.Text = DeviceInformation("Win32_Processor");
//Local Drives Information
textBox3.Text = DeviceInformation("Win32_LogicalDisk");
补充:软件开发 , C# ,