9

dll を使用せずに、VISTA でサポートされているハードディスクのシリアル番号を取得する方法

4

4 に答える 4

14
using System.Management;

public string GetHDDSerial()
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

    foreach (ManagementObject wmi_HD in searcher.Get())
    {
        // get the hardware serial no.
        if (wmi_HD["SerialNumber"] != null)
            return wmi_HD["SerialNumber"].ToString();
    }

    return string.Empty;
}
于 2010-01-29T13:13:32.823 に答える
2

ここからこのコードを試して、うまくいくかどうかお知らせください:

// Namespace Reference
using System.Management;

/// <summary>
/// method to retrieve the selected HDD's serial number
/// </summary>
/// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
/// <returns>the HDD's serial number</returns>
public string GetHDDSerialNumber(string drive)
{
    //check to see if the user provided a drive letter
    //if not default it to "C"
    if (drive == "" || drive == null)
    {
        drive = "C";
    }
    //create our ManagementObject, passing it the drive letter to the
    //DevideID using WQL
    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive +":\"");
    //bind our management object
    disk.Get();
    //return the serial number
    return disk["VolumeSerialNumber"].ToString();
}

編集:それがうまくいかない場合は、CodeProjectからこのコードを試してください:

まず、ハード ドライブに関する情報を格納するクラスを作成しましょう。

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;}
 }
}

次に、Win32_DiskDrive クラスを照会します。

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);
}

ここで、Win32_PhysicalMedia クラスからシリアル番号を抽出する必要があります。

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();
}
于 2009-08-30T13:38:03.630 に答える
0

こんにちは、このリンクを見つけました

それは私のために働いた:

ここにリンクの説明を入力

コードの重要な部分は次のとおりです。

  /// <summary>
  /// return Volume Serial Number from hard drive
  /// </summary>
  /// <param name="strDriveLetter">[optional] Drive letter</param>
  /// <returns>[string] VolumeSerialNumber</returns>
  public string GetVolumeSerial(string strDriveLetter)
  {
      if( strDriveLetter=="" || strDriveLetter==null) 
          strDriveLetter="C";
     ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter +":\"");
     disk.Get();
     return disk["VolumeSerialNumber"].ToString();
  }
于 2013-06-27T12:19:27.233 に答える