有没有更快的方法来获取卷序列号?
您好,我正在使用此代码来生成机器签名。 但执行起来需要相当长的时间。 想知道为什么这么慢? 有什么更快的方法推荐吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
egghead Cafe 使用 System.Management 命名空间。
首先,您需要在vb.net中添加对System.Management dll的引用。 使用“项目”->“添加引用”菜单项。
然后以下代码获取 C 驱动器的驱动器序列号:
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:
这很尴尬。 只需检查“固定”驱动器即可解决性能问题。
This is embarrassing. The performance problem can be solved by simply check for "fixed" drive.
还有一个 Win32 API 调用,但我认为 WMI 是更好的方法,因为它仍然是托管代码。
Win32 API函数: GetVolumeInformation
我找到了eggheadcafe 上的以下函数(它是 C#,但在 VB 中执行应该不成问题。 net) 来提取序列号:
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: