程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> c#獲取硬盤相應序列號 代碼

c#獲取硬盤相應序列號 代碼

編輯:關於C#
 

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()
{
}

}
}
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved