获取硬盘相应序列号

using System;
using Microsoft.Win32;
using System.Globalization;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

namespace HKH.Common
{
/// <summary>
/// Summary description for clsIDE.
/// </summary>
public class clsIDE
{
/// <summary>
/// 获取硬盘相应分区的序列号
/// </summary>
/// <returns></returns>
public static string GetAllSerialNumber()
{
string Dri = "";

System.Management.ManagementClass mo = new System.Management.ManagementClass("Win32_LogicalDisk");

System.Management.ManagementObjectCollection mc = mo.GetInstances();

foreach (System.Management.ManagementObject m in mc)
{
if (Convert.ToString(m.Properties["DriveType"].Value) == "3")
{
Dri = Dri + m.Properties["VolumeSerialNumber"].Value.ToString() + "\n";
}
}

Dri = Dri.Substring(0, Dri.Length - 1);

return Dri;
}

/// <summary>
/// 获取硬盘相应分区的序列号
/// </summary>
/// <param name="Drive">盘符(如 C)</param>
/// <returns></returns>
public static string GetSpecialVolumeSerialNumber(string Drive)
{
string Dri = "";

System.Management.ManagementClass mo = new System.Management.ManagementClass("Win32_LogicalDisk");

System.Management.ManagementObjectCollection mc = mo.GetInstances();

foreach (System.Management.ManagementObject m in mc)
{
if (Convert.ToString(m.Properties["DriveType"].Value) == "3")
{
if (m.Properties["Name"].Value.ToString().ToUpper().Trim().Substring(0, 1) == Drive.ToUpper().Trim())
{
Dri = Dri + m.Properties["VolumeSerialNumber"].Value.ToString();

break;
}
}
}

return Dri;
}

private clsIDE()
{
}

}
}


你可能感兴趣的:(C++,c,C#,Microsoft)