有没有更快的方法来获取卷序列号?

发布于 2024-07-29 04:21:14 字数 928 浏览 3 评论 0原文

您好,我正在使用此代码来生成机器签名。 但执行起来需要相当长的时间。 想知道为什么这么慢? 有什么更快的方法推荐吗?

Public Shared Function DriveSN(ByVal DriveLetter As String) As String
    Dim disk As ManagementObject = New ManagementObject(String.Format("Win32_Logicaldisk='{0}'", DriveLetter))
    Dim VolumeName As String = disk.Properties("VolumeName").Value.ToString()
    Dim SerialNumber As String = disk.Properties("VolumeSerialnumber").Value.ToString()
    Return SerialNumber.Insert(4, "-")
End Function

Private Shared msig As String = Nothing

Public Shared Function MachineSignature() As String
    If msig Is Nothing Then
        Dim list As New List(Of String)
        For Each d As DriveInfo In DriveInfo.GetDrives()
            If (d.IsReady) Then
                list.Add(DriveSN(d.Name.Substring(0, 2)))
            End If
        Next
        msig = String.Join(" & ", list.ToArray())
    End If
    Return msig
End Function

Hi I'm using this code to generate machine signature. But it's take noticeable time to execute. Wonder why it's that slow ? Any faster method recommended ?

Public Shared Function DriveSN(ByVal DriveLetter As String) As String
    Dim disk As ManagementObject = New ManagementObject(String.Format("Win32_Logicaldisk='{0}'", DriveLetter))
    Dim VolumeName As String = disk.Properties("VolumeName").Value.ToString()
    Dim SerialNumber As String = disk.Properties("VolumeSerialnumber").Value.ToString()
    Return SerialNumber.Insert(4, "-")
End Function

Private Shared msig As String = Nothing

Public Shared Function MachineSignature() As String
    If msig Is Nothing Then
        Dim list As New List(Of String)
        For Each d As DriveInfo In DriveInfo.GetDrives()
            If (d.IsReady) Then
                list.Add(DriveSN(d.Name.Substring(0, 2)))
            End If
        Next
        msig = String.Join(" & ", list.ToArray())
    End If
    Return msig
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罪#恶を代价 2024-08-05 04:21:14

egghead Cafe 使用 System.Management 命名空间。

首先,您需要在vb.net中添加对System.Management dll的引用。 使用“项目”->“添加引用”菜单项。

然后以下代码获取 C 驱动器的驱动器序列号:

Dim drive As String = "C"
Dim disk As System.Management.ManagementObject = _
       New System.Management.ManagementObject _
       ("win32_logicaldisk.deviceid=""" + drive + ":""")
disk.Get()
Dim SerialNumber as String = disk("VolumeSerialNumber").ToString()

There is a .Net way also from egghead cafe using the System.Management namespace.

Firstly, you need to add the reference to the System.Management dll in vb.net. Using the Project->Add Reference menu item.

Then the following code gets the drive serial number for the C drive:

Dim drive As String = "C"
Dim disk As System.Management.ManagementObject = _
       New System.Management.ManagementObject _
       ("win32_logicaldisk.deviceid=""" + drive + ":""")
disk.Get()
Dim SerialNumber as String = disk("VolumeSerialNumber").ToString()
稳稳的幸福 2024-08-05 04:21:14

这很尴尬。 只需检查“固定”驱动器即可解决性能问题。

Public Shared Function MachineSignature() As String
    If msig Is Nothing Then
        Dim list As New List(Of String)
        For Each d As DriveInfo In DriveInfo.GetDrives()
            If (d.DriveType = DriveType.Fixed) AndAlso (d.IsReady) Then  ' <-- check for DriveType'
                list.Add(DriveSN(d.Name.Substring(0, 2)))
            End If
        Next
        msig = String.Join(" & ", list.ToArray())
    End If
    Return msig
End Function

This is embarrassing. The performance problem can be solved by simply check for "fixed" drive.

Public Shared Function MachineSignature() As String
    If msig Is Nothing Then
        Dim list As New List(Of String)
        For Each d As DriveInfo In DriveInfo.GetDrives()
            If (d.DriveType = DriveType.Fixed) AndAlso (d.IsReady) Then  ' <-- check for DriveType'
                list.Add(DriveSN(d.Name.Substring(0, 2)))
            End If
        Next
        msig = String.Join(" & ", list.ToArray())
    End If
    Return msig
End Function
或十年 2024-08-05 04:21:14

还有一个 Win32 API 调用,但我认为 WMI 是更好的方法,因为它仍然是托管代码。

Win32 API函数: GetVolumeInformation

我找到了eggheadcafe 上的以下函数(它是 C#,但在 VB 中执行应该不成问题。 net) 来提取序列号:

public string GetVolumeSerial(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256); // Label
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256); // File System Name
strDriveLetter+=":\\"; // fix up the passed-in drive letter for the API call
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);

return Convert.ToString(serNum);
}

There is a Win32 API call to this also, but i think WMI is the better way since it's still managed code.

Win32 API function: GetVolumeInformation

I found the following function on eggheadcafe (it's C#, but should be not problem to do in vb.net) to extract the Serial number:

public string GetVolumeSerial(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256); // Label
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256); // File System Name
strDriveLetter+=":\\"; // fix up the passed-in drive letter for the API call
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);

return Convert.ToString(serNum);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文