获取硬盘相应序列号

usingSystem;usingMicrosoft.Win32;usingSystem.GlPython
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

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,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/2383

发表评论