请问如何用C#取得当前硬盘的序列号?

解决方案 »

  1.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=6977HLMY-ELPN-4KIR-BI89-7YS2LNENT5HR
      

  2.   

    http://blog.sunmast.com/Sunmast/archive/2005/06/06/2632.aspx
      

  3.   

    using System;
    using System.Management;public class Test
    {
      static void Main()
      {
        Console.WriteLine("My HardDisk is :");
        GetHd();
      }  public static void GetHd()
      {
        ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();
        wmiSearcher.Query = new SelectQuery("Win32_DiskDrive", "", new string[]{"PNPDeviceID"});
        ManagementObjectCollection myCollection = wmiSearcher.Get();
        ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator();
        while(em.MoveNext())
        {
          ManagementBaseObject mo = em.Current;
          Console.WriteLine(mo.Properties["PNPDeviceID"].Value.ToString().Trim());
        }
      }
    }/* 程序输出: (我有2块硬盘)
    My HardDisk is :
    IDE\DISKIBM-DTLA-307030_________________________TX4OA60A\5&230D196C&0&0.0.0
    IDE\DISKQUANTUM_FIREBALL_CR6.4A_________________A5U.1200\3231393633313639353730312020202020202020
    */
      

  4.   

    [DllImport("kernel32.dll")]
    private static extern int GetVolumeInformation( string  lpRootPathName,
    string  lpVolumeNameBuffer,
    int  nVolumeNameSize,
    ref int  lpVolumeSerialNumber,
    int  lpMaximumComponentLength,
    int  lpFileSystemFlags,
    string  lpFileSystemNameBuffer,
    int  nFileSystemNameSize
    ); /// <summary>
    /// 获得盘符为drvID的硬盘序列号,缺省为C
    /// </summary>
    /// <param name="drvID"></param>
    /// <returns></returns>
    public string HDVal(string drvID)
    {
    const int MAX_FILENAME_LEN = 256;
    int retVal = 0;
    int a =0;
    int b =0;
    string str1 = null;
    string str2 = null;
    int i = GetVolumeInformation(
    drvID + @":\",
    str1,
    MAX_FILENAME_LEN,
    ref retVal,
    a,
    b,
    str2,
    MAX_FILENAME_LEN
    );
    return retVal.ToString();
    } public string HDVal()
    {
    const int MAX_FILENAME_LEN = 256;
    int retVal = 0;
    int a =0;
    int b =0;
    string str1 = null;
    string str2 = null;
    int i = GetVolumeInformation(
    "c:\\",
    str1,
    MAX_FILENAME_LEN,
    ref retVal,
    a,
    b,
    str2,
    MAX_FILENAME_LEN
    );
    return retVal.ToString();
    }
      

  5.   

    以上答案皆有问题:取的是硬盘的卷标号,格式化后都变了。
    我自己有用C++写了个DLL,可以读出硬盘物理序列号,不知道楼主是不是要这样的??
      

  6.   

    to weisunding(鼎鼎) :是的,就是要读出硬盘物理序列号
      

  7.   

    TO  weisunding(鼎鼎):不要误导新手哦~~~~
      

  8.   

    以上答案皆有问题:取的是硬盘的卷标号,格式化后都变了。
    我自己有用C++写了个DLL,可以读出硬盘物理序列号,不知道楼主是不是要这样的??
    -------------------------------------------------------------
    /* 程序输出: (我有2块硬盘)
    My HardDisk is :
    IDE\DISKIBM-DTLA-307030_________________________TX4OA60A\5&230D196C&0&0.0.0
    IDE\DISKQUANTUM_FIREBALL_CR6.4A_________________A5U.1200\3231393633313639353730312020202020202020
    */
    这难道是硬盘的卷标号?