先秀下效果图。要获取主机信息包括:OS名称、版本、OS 制造商、系统名称、Windows 目录、系统目录、启动设备、区域设置、时区、总的物理内存、可用物理内存、总的虚拟内存、可用虚拟内存和页面文件空间。
本系统计划采用如下的结构(懒得画图,直接用语言描述):一个类库,用于获取计算机的信息。解决方案命名为:ComputerInfo,里面包含一些类,以后每个小功能都会增加一个类,现在刚刚做了获取计算机的信息的类,命名为:SystemInfo,类图如下。然后每个小功能为一个解决方案里面的一个窗体。
public class SystemInfo
{
属性#region 属性
OS名称#region OS名称
string strOSName = string.Empty; //OS名称
public string OSName
{
get { return strOSName; }
set { strOSName = value; }
}
#endregion
OS版本#region OS版本
string strOSVersion = string.Empty; //OS版本
public string OSVersion
{
get { return strOSVersion; }
set { strOSVersion = value; }
}
#endregion
OS制造商#region OS制造商
string strOSManufacturer = string.Empty; //OS制造商
public string OSManufacturer
{
get { return strOSManufacturer; }
set { strOSManufacturer = value; }
}
#endregion
Windows 目录#region Windows 目录
string strWindowsDirectory = string.Empty;
public string WindowsDirectory
{
get { return strWindowsDirectory; }
set { strWindowsDirectory = value; }
}
#endregion
系统目录#region 系统目录
string strSystemDirectory = string.Empty; //系统目录
public string SystemDirectory
{
get { return strSystemDirectory; }
set { strSystemDirectory = value; }
}
#endregion
启动设备#region 启动设备
string strBootDevice = string.Empty; //启动设备
public string BootDevice
{
get { return strBootDevice; }
set { strBootDevice = value; }
}
#endregion
地区#region 地区
string strCountry = string.Empty; //地区
public string Country
{
get { return strCountry; }
set { strCountry = value; }
}
#endregion
时区#region 时区
string strTimeZone = string.Empty; //时区
public string TimeZone
{
get { return strTimeZone; }
set { strTimeZone = value; }
}
#endregion
总的物理内存#region 总的物理内存
string strTotalVisibleMemorySize = string.Empty; //总的物理内存
public string TotalVisibleMemorySize
{
get { return strTotalVisibleMemorySize; }
set { strTotalVisibleMemorySize = value; }
}
#endregion
可用物理内存#region 可用物理内存
string strFreePhysicalMemory = string.Empty; //可用物理内存
public string FreePhysicalMemory
{
get { return strFreePhysicalMemory; }
set { strFreePhysicalMemory = value; }
}
#endregion
总的虚拟内存#region 总的虚拟内存
string strTotalVirtualMemorySize = string.Empty; //总的虚拟内存
public string TotalVirtualMemorySize
{
get { return strTotalVirtualMemorySize; }
set { strTotalVirtualMemorySize = value; }
}
#endregion
可用虚拟内存#region 可用虚拟内存
string strFreeVirtualMemory = string.Empty; //可用虚拟内存
public string FreeVirtualMemory
{
get { return strFreeVirtualMemory; }
set { strFreeVirtualMemory = value; }
}
#endregion
页面文件大小#region 页面文件大小
string strSizeStoredInPagingFiles = string.Empty; //页面文件大小
public string SizeStoredInPagingFiles
{
get { return strSizeStoredInPagingFiles; }
set { strSizeStoredInPagingFiles = value; }
}
#endregion
#endregion
构造函数#region 构造函数
public SystemInfo()
{
GetSystemInfo();
}
#endregion
GetSystemInfo#region GetSystemInfo
private void GetSystemInfo()
{
OSVersion = Environment.OSVersion.ToString(); //通过系统自带的Environment这个类来获取 系统的版本
SystemName = Environment.MachineName; //通过系统自带的Environment这个类来获取 系统的名称
Country = System.Globalization.CultureInfo.CurrentCulture.DisplayName; //获取区域设置
TimeZone = System.TimeZone.CurrentTimeZone.StandardName; //获取时区设置
//实例化一个ManagementClass类,并将Win32_OperatingSystem作为参数传递进去,
//这样就可以查询Win32_OperatingSystem这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_OperatingSystem");
//获取Win32_OperatingSystem这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
//对Win32_OperatingSystem这个类进行遍历
foreach (ManagementObject mObject in moCollection)
{
OSName = mObject["Caption"].ToString(); //获取OS 名称
OSManufacturer = mObject["Manufacturer"].ToString(); //获取 OS 制造商
SystemName = mObject["CSName"].ToString(); //获取系统名称
WindowsDirectory = mObject["WindowsDirectory"].ToString(); //获取Windows 目录
SystemDirectory = mObject["SystemDirectory"].ToString(); //获取系统目录
BootDevice = mObject["BootDevice"].ToString(); //获取启动设备
TotalVisibleMemorySize = ((ulong)mObject["TotalVisibleMemorySize"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取总的物理内存
FreePhysicalMemory = ((ulong)mObject["FreePhysicalMemory"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用物理内存
TotalVirtualMemorySize = ((ulong)mObject["TotalVirtualMemorySize"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取总的虚拟内存
FreeVirtualMemory = ((ulong)mObject["FreeVirtualMemory"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用虚拟内存
SizeStoredInPagingFiles = ((ulong)mObject["SizeStoredInPagingFiles"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取页面文件大小
}
}
#endregion
}
{
属性#region 属性
OS名称#region OS名称
string strOSName = string.Empty; //OS名称
public string OSName
{
get { return strOSName; }
set { strOSName = value; }
}
#endregion
OS版本#region OS版本
string strOSVersion = string.Empty; //OS版本
public string OSVersion
{
get { return strOSVersion; }
set { strOSVersion = value; }
}
#endregion
OS制造商#region OS制造商
string strOSManufacturer = string.Empty; //OS制造商
public string OSManufacturer
{
get { return strOSManufacturer; }
set { strOSManufacturer = value; }
}
#endregion
Windows 目录#region Windows 目录
string strWindowsDirectory = string.Empty;
public string WindowsDirectory
{
get { return strWindowsDirectory; }
set { strWindowsDirectory = value; }
}
#endregion
系统目录#region 系统目录
string strSystemDirectory = string.Empty; //系统目录
public string SystemDirectory
{
get { return strSystemDirectory; }
set { strSystemDirectory = value; }
}
#endregion
启动设备#region 启动设备
string strBootDevice = string.Empty; //启动设备
public string BootDevice
{
get { return strBootDevice; }
set { strBootDevice = value; }
}
#endregion
地区#region 地区
string strCountry = string.Empty; //地区
public string Country
{
get { return strCountry; }
set { strCountry = value; }
}
#endregion
时区#region 时区
string strTimeZone = string.Empty; //时区
public string TimeZone
{
get { return strTimeZone; }
set { strTimeZone = value; }
}
#endregion
总的物理内存#region 总的物理内存
string strTotalVisibleMemorySize = string.Empty; //总的物理内存
public string TotalVisibleMemorySize
{
get { return strTotalVisibleMemorySize; }
set { strTotalVisibleMemorySize = value; }
}
#endregion
可用物理内存#region 可用物理内存
string strFreePhysicalMemory = string.Empty; //可用物理内存
public string FreePhysicalMemory
{
get { return strFreePhysicalMemory; }
set { strFreePhysicalMemory = value; }
}
#endregion
总的虚拟内存#region 总的虚拟内存
string strTotalVirtualMemorySize = string.Empty; //总的虚拟内存
public string TotalVirtualMemorySize
{
get { return strTotalVirtualMemorySize; }
set { strTotalVirtualMemorySize = value; }
}
#endregion
可用虚拟内存#region 可用虚拟内存
string strFreeVirtualMemory = string.Empty; //可用虚拟内存
public string FreeVirtualMemory
{
get { return strFreeVirtualMemory; }
set { strFreeVirtualMemory = value; }
}
#endregion
页面文件大小#region 页面文件大小
string strSizeStoredInPagingFiles = string.Empty; //页面文件大小
public string SizeStoredInPagingFiles
{
get { return strSizeStoredInPagingFiles; }
set { strSizeStoredInPagingFiles = value; }
}
#endregion
#endregion
构造函数#region 构造函数
public SystemInfo()
{
GetSystemInfo();
}
#endregion
GetSystemInfo#region GetSystemInfo
private void GetSystemInfo()
{
OSVersion = Environment.OSVersion.ToString(); //通过系统自带的Environment这个类来获取 系统的版本
SystemName = Environment.MachineName; //通过系统自带的Environment这个类来获取 系统的名称
Country = System.Globalization.CultureInfo.CurrentCulture.DisplayName; //获取区域设置
TimeZone = System.TimeZone.CurrentTimeZone.StandardName; //获取时区设置
//实例化一个ManagementClass类,并将Win32_OperatingSystem作为参数传递进去,
//这样就可以查询Win32_OperatingSystem这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_OperatingSystem");
//获取Win32_OperatingSystem这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
//对Win32_OperatingSystem这个类进行遍历
foreach (ManagementObject mObject in moCollection)
{
OSName = mObject["Caption"].ToString(); //获取OS 名称
OSManufacturer = mObject["Manufacturer"].ToString(); //获取 OS 制造商
SystemName = mObject["CSName"].ToString(); //获取系统名称
WindowsDirectory = mObject["WindowsDirectory"].ToString(); //获取Windows 目录
SystemDirectory = mObject["SystemDirectory"].ToString(); //获取系统目录
BootDevice = mObject["BootDevice"].ToString(); //获取启动设备
TotalVisibleMemorySize = ((ulong)mObject["TotalVisibleMemorySize"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取总的物理内存
FreePhysicalMemory = ((ulong)mObject["FreePhysicalMemory"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用物理内存
TotalVirtualMemorySize = ((ulong)mObject["TotalVirtualMemorySize"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取总的虚拟内存
FreeVirtualMemory = ((ulong)mObject["FreeVirtualMemory"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用虚拟内存
SizeStoredInPagingFiles = ((ulong)mObject["SizeStoredInPagingFiles"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取页面文件大小
}
}
#endregion
}
再启动一个VS2008 创建一个解决方案命名为:HostPerformance,添加上面那个类库的引用,如下图
添加一个窗体,命名为:SystemInfoForm,往窗体里面添加一个ListView控件,代码如下:
SystemInfo Form
public partial class SystemInfoForm : Form
{
public SystemInfoForm()
{
InitializeComponent();
}
private void SystemInfoForm_Load(object sender, EventArgs e)
{
//实例化一个ComputerInfo.SystemInfo类。
ComputerInfo.SystemInfo systemInfo = new ComputerInfo.SystemInfo();
定义一些需要用到的项#region 定义一些需要用到的项
ListViewItem lviOSName = new ListViewItem(new string[]{ "OS 名称",systemInfo.OSName});
ListViewItem lviOSVersion = new ListViewItem(new string[] { "版本", systemInfo.OSVersion });
ListViewItem lviOSManufacturer = new ListViewItem(new string[] { "OS 制造商", systemInfo.OSManufacturer });
ListViewItem lviSystemName = new ListViewItem(new string[] { "系统名称", systemInfo.SystemName });
ListViewItem lviWindowsDirectory = new ListViewItem(new string[] { "Windows 目录", systemInfo.WindowsDirectory });
ListViewItem lviSystemDirectory = new ListViewItem(new string[] { "系统目录", systemInfo.SystemDirectory });
ListViewItem lviBootDevice = new ListViewItem(new string[] { "启动设备", systemInfo.BootDevice });
ListViewItem lviCountry = new ListViewItem(new string[] { "区域设置", systemInfo.Country });
ListViewItem lviTimeZone = new ListViewItem(new string[] { "时区", systemInfo.TimeZone });
ListViewItem lviTotalVisibleMemorySize = new ListViewItem(new string[] { "总的物理内存", systemInfo.TotalVisibleMemorySize });
ListViewItem lviFreePhysicalMemory = new ListViewItem(new string[] { "可用物理内存", systemInfo.FreePhysicalMemory });
ListViewItem lviTotalVirtualMemorySize = new ListViewItem(new string[] { "总的虚拟内存", systemInfo.TotalVirtualMemorySize });
ListViewItem lviFreeVirtualMemory = new ListViewItem(new string[] { "可用虚拟内存", systemInfo.FreeVirtualMemory });
ListViewItem lviSizeStoredInPagingFiles = new ListViewItem(new string[] { "页面文件空间", systemInfo.SizeStoredInPagingFiles });
#endregion
将项添加到ListView控件里面#region 将项添加到ListView控件里面
lvHostInformation.Items.Add(lviOSName);
lvHostInformation.Items.Add(lviOSVersion);
lvHostInformation.Items.Add(lviOSManufacturer);
lvHostInformation.Items.Add(lviSystemName);
lvHostInformation.Items.Add(lviWindowsDirectory);
lvHostInformation.Items.Add(lviSystemDirectory);
lvHostInformation.Items.Add(lviBootDevice);
lvHostInformation.Items.Add(lviCountry);
lvHostInformation.Items.Add(lviTimeZone);
lvHostInformation.Items.Add(lviTotalVisibleMemorySize);
lvHostInformation.Items.Add(lviFreePhysicalMemory);
lvHostInformation.Items.Add(lviTotalVirtualMemorySize);
lvHostInformation.Items.Add(lviFreeVirtualMemory);
lvHostInformation.Items.Add(lviSizeStoredInPagingFiles);
#endregion
设置ListView的样式以及列#region 设置ListView的样式以及列
lvHostInformation.View = View.Details;
lvHostInformation.Columns.Add("项",150);
lvHostInformation.Columns.Add("值",300);
lvHostInformation.FullRowSelect = true;
lvHostInformation.GridLines = true;
#endregion
}
}
public partial class SystemInfoForm : Form
{
public SystemInfoForm()
{
InitializeComponent();
}
private void SystemInfoForm_Load(object sender, EventArgs e)
{
//实例化一个ComputerInfo.SystemInfo类。
ComputerInfo.SystemInfo systemInfo = new ComputerInfo.SystemInfo();
定义一些需要用到的项#region 定义一些需要用到的项
ListViewItem lviOSName = new ListViewItem(new string[]{ "OS 名称",systemInfo.OSName});
ListViewItem lviOSVersion = new ListViewItem(new string[] { "版本", systemInfo.OSVersion });
ListViewItem lviOSManufacturer = new ListViewItem(new string[] { "OS 制造商", systemInfo.OSManufacturer });
ListViewItem lviSystemName = new ListViewItem(new string[] { "系统名称", systemInfo.SystemName });
ListViewItem lviWindowsDirectory = new ListViewItem(new string[] { "Windows 目录", systemInfo.WindowsDirectory });
ListViewItem lviSystemDirectory = new ListViewItem(new string[] { "系统目录", systemInfo.SystemDirectory });
ListViewItem lviBootDevice = new ListViewItem(new string[] { "启动设备", systemInfo.BootDevice });
ListViewItem lviCountry = new ListViewItem(new string[] { "区域设置", systemInfo.Country });
ListViewItem lviTimeZone = new ListViewItem(new string[] { "时区", systemInfo.TimeZone });
ListViewItem lviTotalVisibleMemorySize = new ListViewItem(new string[] { "总的物理内存", systemInfo.TotalVisibleMemorySize });
ListViewItem lviFreePhysicalMemory = new ListViewItem(new string[] { "可用物理内存", systemInfo.FreePhysicalMemory });
ListViewItem lviTotalVirtualMemorySize = new ListViewItem(new string[] { "总的虚拟内存", systemInfo.TotalVirtualMemorySize });
ListViewItem lviFreeVirtualMemory = new ListViewItem(new string[] { "可用虚拟内存", systemInfo.FreeVirtualMemory });
ListViewItem lviSizeStoredInPagingFiles = new ListViewItem(new string[] { "页面文件空间", systemInfo.SizeStoredInPagingFiles });
#endregion
将项添加到ListView控件里面#region 将项添加到ListView控件里面
lvHostInformation.Items.Add(lviOSName);
lvHostInformation.Items.Add(lviOSVersion);
lvHostInformation.Items.Add(lviOSManufacturer);
lvHostInformation.Items.Add(lviSystemName);
lvHostInformation.Items.Add(lviWindowsDirectory);
lvHostInformation.Items.Add(lviSystemDirectory);
lvHostInformation.Items.Add(lviBootDevice);
lvHostInformation.Items.Add(lviCountry);
lvHostInformation.Items.Add(lviTimeZone);
lvHostInformation.Items.Add(lviTotalVisibleMemorySize);
lvHostInformation.Items.Add(lviFreePhysicalMemory);
lvHostInformation.Items.Add(lviTotalVirtualMemorySize);
lvHostInformation.Items.Add(lviFreeVirtualMemory);
lvHostInformation.Items.Add(lviSizeStoredInPagingFiles);
#endregion
设置ListView的样式以及列#region 设置ListView的样式以及列
lvHostInformation.View = View.Details;
lvHostInformation.Columns.Add("项",150);
lvHostInformation.Columns.Add("值",300);
lvHostInformation.FullRowSelect = true;
lvHostInformation.GridLines = true;
#endregion
}
}
至此,获取计算机信息的小功能就做完了,代码都已经贴上来了,大家可以自己弄到VS2008里面试试。
未完,待续~~下期预告:2.获取性能信息--CPU