harddrivesample1.cs

来自「显示HD信息 说明: 需要win2000或者winxp的DDK」· CS 代码 · 共 88 行

CS
88
字号
using System;
using System.Collections;
using System.Management;

namespace HardDriveSample1
{
	class HardDrive
	{
		private string model = null;
		private string type = null;
		private string serialNo = null;

		public string Model
		{
			get {return model;}
			set {model = value;}
		}

		public string Type
		{
			get {return type;}
			set {type = value;}
		}

		public string SerialNo
		{
			get {return serialNo;}
			set {serialNo = value;}
		}
	}

	class TestProgram
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			ArrayList hdCollection = new ArrayList();

			ManagementObjectSearcher searcher = new
				ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

			foreach(ManagementObject wmi_HD in searcher.Get())
			{
				HardDrive hd = new HardDrive();
				hd.Model	= wmi_HD["Model"].ToString();
				hd.Type		= wmi_HD["InterfaceType"].ToString();

				hdCollection.Add(hd);
			}

			searcher = new
				ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

			int i = 0;
			foreach(ManagementObject wmi_HD in searcher.Get())
			{
				// get the hard drive from collection
				// using index
				HardDrive hd = (HardDrive)hdCollection[i];

				// get the hardware serial no.
				if (wmi_HD["SerialNumber"] == null)
					hd.SerialNo = "None";
				else
					hd.SerialNo = wmi_HD["SerialNumber"].ToString();

				++i;
			}

			// Display available hard drives
			foreach(HardDrive hd in hdCollection)
			{
				Console.WriteLine("Model\t\t: " + hd.Model);
				Console.WriteLine("Type\t\t: " + hd.Type);
				Console.WriteLine("Serial No.\t: " + hd.SerialNo);
				Console.WriteLine();
			}

			// Pause application
			Console.WriteLine("Press [Enter] to exit...");
			Console.ReadLine();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?