获取硬盘相应序列号

2021-03-22 15:38:07 浏览数 (1)

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

 } }

本文由来源 21aspnet,由 javajgs_com 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。

0 人点赞