【问题标题】:Retrieving disc serial id gives me different values using FSO or ManagementObject使用 FSO 或 ManagementObject 检索磁盘序列号给了我不同的值
【发布时间】:2014-04-22 14:21:33
【问题描述】:

我有一个旧的 vb 应用程序,它使用此代码检索磁盘 ID。

Dim FSO As New Scripting.FileSystemObject
Dim Dr As Scripting.Drive
Dim id_convertido As Long = 0
Dim sRutaAplicacion As String = Application.StartupPath
Dim Valor As Integer = sRutaAplicacion.IndexOf(":\")
If Valor <> -1 Then
    Dim RaizAplicacion As String
    RaizAplicacion = Mid(sRutaAplicacion, 1, Valor)
    For Each Dr In FSO.Drives
        If Dr.DriveLetter = RaizAplicacion Then
             Dim idDisco As String
             idDisco = Dr.SerialNumber
             id_convertido = (Microsoft.VisualBasic.Right(idDisco, 8))
             Return id_convertido
        End If
    Next
End If

我想在c#中实现同样的功能,所以我找到了这段代码:

string HDD = System.Environment.CurrentDirectory.Substring(0, 1);
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + HDD + ":\"");
disk.Get();
return disk["VolumeSerialNumber"].ToString();

但我得到不同的值。在我的第一个代码中,“idDisco”是“876823094”,但在 c# 中,这个值是“34434236”。

两者都在检查磁盘 c:

有什么线索吗?

非常感谢!

【问题讨论】:

    标签: c# vb.net disk serial-number


    【解决方案1】:

    在我的第一个代码中,“idDisco”是“876823094”,但在 c# 中,这个值是“34434236”

    同样的值,16进制的876823094是0x34434236。使用 Calc.exe、View + Programmer 亲自查看。如果你想重现 FSO 给你的相同数字,那么你必须做同样的事情并转换为基数 10。像这样:

    string hex = disk["VolumeSerialNumber"].ToString();
    int value = int.Parse(hex, System.Globalization.NumberStyles.HexNumber, null);
    string fso = value.ToString();
    

    【讨论】:

      【解决方案2】:

      查看this thread中的解决方案。

      自定义类HardDrive用于存储检索到的信息。

      获取硬盘型号

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

      (最低)类硬盘

      public class HardDrive
      {
          public string Model { get; set }
          public string Type { get; set; }
      }
      

      获取序列号

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

      【讨论】:

      • HardDrive 已添加。
      • 好吧,它有效,但它不能解决我的问题。我使用这种方法获得的序列号与我提供的 2 个代码不同。这是我的问题,我的程序使用我提供的 vb 代码获取“磁盘 ID”,以便识别我的程序是否在使用相同登录名的多台电脑上使用。无论如何,我需要设法获得相同的 id,但使用 c#。
      • 我使用旧的 vb 代码制作了一个 .dll,它可以正常工作,所以问题解决了,谢谢!
      猜你喜欢
      • 2015-03-27
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 2018-05-15
      • 2017-05-04
      • 1970-01-01
      相关资源
      最近更新 更多