Private Function 硬盤序列號() As String
創(chuàng)新互聯公司主營高唐網站建設的網絡公司,主營網站建設方案,重慶APP軟件開發(fā),高唐h5重慶小程序開發(fā)公司搭建,高唐網站營銷推廣歡迎高唐等地區(qū)企業(yè)咨詢
Try
Dim myInfo As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("HARDWARE\DEVICEMAP\Scsi\Scsi Port 0\Scsi Bus 1\Target Id 0\Logical Unit Id 0")
硬盤序列號 = Trim(myInfo.GetValue("SerialNumber"))
Catch
Try
Dim myInfo As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("HARDWARE\DEVICEMAP\Scsi\Scsi Port 1\Scsi Bus 1\Target Id 0\Logical Unit Id 0")
硬盤序列號 = Trim(myInfo.GetValue("SerialNumber"))
Catch
硬盤序列號 = ""
End Try
End Try
End Function
試下,如果返回為空,則表示失敗。
在本機win8win8.1有效,不過好像在有些機器上沒用。
首先使用 System.IO.DriveInfo.GetDrives()獲取System.IO.DriveInfo,存入ds()
然后遍歷ds,獲取各個信息部分。
Dim ds() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
For i As Integer = 0 To ds.Length - 1
TextBox1.Text = TextBox1.Text + ds(i).DriveType.ToString + " " '驅動器類型
TextBox1.Text = TextBox1.Text + ds(i).Name + " " '盤符(驅動器名)
TextBox1.Text = TextBox1.Text + ds(i).IsReady.ToString + " " '是否就緒
If ds(i).IsReady = True Then
TextBox1.Text = TextBox1.Text + ds(i).VolumeLabel + " " '卷標
TextBox1.Text = TextBox1.Text + ds(i).TotalSize.ToString + " " '驅動器容量
TextBox1.Text = TextBox1.Text + ds(i).TotalFreeSpace.ToString '驅動器可用容量
End If
TextBox1.Text = TextBox1.Text + vbNewLine
Next
vb6讀取win10下硬盤序列號方法如下:
1、是指硬盤物理序列號,格式化沒有變化。
2、支持vista 及win10系統(tǒng)。
3、支持多塊硬盤(有的電腦裝有幾塊硬盤)
4、支持串口及并口硬盤。
5、最好是源碼或dll 等,代碼如下:
Visual Basic code
'-------------------添加類模塊clsMainInfo-------------------------
Option Explicit
Private Const VER_PLATFORM_WIN32S = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2
Private Const DFP_RECEIVE_DRIVE_DATA = H7C088
Private Const FILE_SHARE_READ = H1
Private Const FILE_SHARE_WRITE = H2
Private Const GENERIC_READ = H80000000
Private Const GENERIC_WRITE = H40000000
Private Const OPEN_EXISTING = 3
Private Const Create_NEW = 1
Private Enum HDINFO
HD_MODEL_NUMBER
HD_SERIAL_NUMBER
HD_FIRMWARE_REVISION
End Enum
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Private Type IDEREGS
bFeaturesReg As Byte
bSectorCountReg As Byte
bSectorNumberReg As Byte
bCylLowReg As Byte
bCylHighReg As Byte
bDriveHeadReg As Byte
bCommandReg As Byte
bReserved As Byte
End Type
Private Type SENDCMDINPARAMS
cBufferSize As Long
irDriveRegs As IDEREGS
bDriveNumber As Byte
bReserved(1 To 3) As Byte
dwReserved(1 To 4) As Long
End Type
Private Type DRIVERSTATUS
bDriveError As Byte
bIDEStatus As Byte
bReserved(1 To 2) As Byte
dwReserved(1 To 2) As Long
End Type
Private Type SENDCMDOUTPARAMS
cBufferSize As Long
DStatus As DRIVERSTATUS
bBuffer(1 To 512) As Byte
End Type
Private Declare Function GetVersionEx _
Lib "kernel32" Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function CreateFile _
Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle _
Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function DeviceIoControl _
Lib "kernel32" _
(ByVal hDevice As Long, _
ByVal dwIoControlCode As Long, _
lpInBuffer As Any, _
ByVal nInBufferSize As Long, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Long, _
lpBytesReturned As Long, _
ByVal lpOverlapped As Long) As Long
Private Declare Sub ZeroMemory _
Lib "kernel32" Alias "RtlZeroMemory" _
(dest As Any, _
ByVal numBytes As Long)
Private Declare Sub CopyMemory _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Declare Function GetLastError _
Lib "kernel32" () As Long
Private mvarCurrentDrive As Byte
Private mvarPlatform As String
Public Function GetModelNumber() As String
GetModelNumber = CmnGetHDData(HD_MODEL_NUMBER)
End Function
Public Function GetSerialNumber() As String
GetSerialNumber = CmnGetHDData(HD_SERIAL_NUMBER)
End Function
Public Function GetFirmwareRevision() As String
GetFirmwareRevision = CmnGetHDData(HD_FIRMWARE_REVISION)
End Function
Public Property Let CurrentDrive(ByVal vData As Byte)
If vData 0 Or vData 3 Then
? Err.Raise 10000, , "Illegal Drive Number"
End If
mvarCurrentDrive = vData
End Property
Public Property Get CurrentDrive() As Byte
CurrentDrive = mvarCurrentDrive
End Property
Public Property Get Platform() As String
Platform = mvarPlatform
End Property
Private Sub Class_Initialize()
Dim OS As OSVERSIONINFO
OS.dwOSVersionInfoSize = Len(OS)
Call GetVersionEx(OS)
mvarPlatform = "Unk"
Select Case OS.dwPlatformId
? Case Is = VER_PLATFORM_WIN32S
? ? ? mvarPlatform = "32S"
? Case Is = VER_PLATFORM_WIN32_WINDOWS
? ? ? If OS.dwMinorVersion = 0 Then
? ? ? ? ? mvarPlatform = "W95"
? ? ? Else
? ? ? ? ? mvarPlatform = "W98"
? ? ? End If
? Case Is = VER_PLATFORM_WIN32_NT
? ? ? mvarPlatform = "WNT"
End Select
End Sub
Private Function CmnGetHDData(hdi As HDINFO) As String
Dim bin As SENDCMDINPARAMS
Dim bout As SENDCMDOUTPARAMS
Dim hdh As Long
Dim br As Long
Dim ix As Long
Dim hddfr As Long
Dim hddln As Long
Dim s As String
Select Case hdi
? Case HD_MODEL_NUMBER
? ? ? hddfr = 55
? ? ? hddln = 40
? Case HD_SERIAL_NUMBER
? ? ? hddfr = 21
? ? ? hddln = 20
? Case HD_FIRMWARE_REVISION
? ? ? hddfr = 47
? ? ? hddln = 8
? Case Else
? ? ? Err.Raise 10001, "Illegal HD Data type"
End Select
Select Case mvarPlatform
? Case "WNT"
? ? ? hdh = CreateFile("\\.\PhysicalDrive" mvarCurrentDrive, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
? Case "W95", "W98"
? ? ? hdh = CreateFile("\\.\Smartvsd", 0, 0, 0, Create_NEW, 0, 0)
? Case Else
? ? ? Err.Raise 10002, , "Illegal platform (only WNT, W98 or W95)"
End Select
If hdh = 0 Then
? Err.Raise 10003, , "Error on CreateFile"
End If
ZeroMemory bin, Len(bin)
ZeroMemory bout, Len(bout)
With bin
? .bDriveNumber = mvarCurrentDrive
? .cBufferSize = 512
? With .irDriveRegs
? ? ? If (mvarCurrentDrive And 1) Then
? ? ? ? ? .bDriveHeadReg = HB0
? ? ? Else
? ? ? ? ? .bDriveHeadReg = HA0
? ? ? End If
? ? ? .bCommandReg = HEC
? ? ? .bSectorCountReg = 1
? ? ? .bSectorNumberReg = 1
? End With
End With
DeviceIoControl hdh, DFP_RECEIVE_DRIVE_DATA, bin, Len(bin), bout, Len(bout), br, 0
s = vbNullString
For ix = hddfr To hddfr + hddln - 1 Step 2
? If bout.bBuffer(ix + 1) = 0 Then Exit For
? s = s Chr(bout.bBuffer(ix + 1))
? If bout.bBuffer(ix) = 0 Then Exit For
? s = s Chr(bout.bBuffer(ix))
Next ix
CloseHandle hdh
CmnGetHDData = Trim(s)
End Function
Visual Basic code
Option Explicit
'純vb的獲取硬盤序列號代碼 (摘自枕善居)
'窗體放置1個ComBox,命名為cbDrive,1個ListBox,命名為lstMain,一個CommandButton,命名為cmdGo,添加如下代碼
Dim h As clsMainInfo
Private Sub cmdGo_Click()
Dim hT As Long
Dim uW() As Byte
Dim dW() As Byte
Dim pW() As Byte
Set h = New clsMainInfo
With h
? .CurrentDrive = Val(cbDrive.Text)
? ?lstMain.Clear
? ?lstMain.AddItem "當前驅動器: " .CurrentDrive
? ?lstMain.AddItem ""
? ?lstMain.AddItem "硬盤型號: " .GetModelNumber
? ?lstMain.AddItem "序列號: " .GetSerialNumber
? ?lstMain.AddItem "固件版本: " .GetFirmwareRevision
End With
Set h = Nothing
End Sub
Private Sub Form_Load()
cbDrive.AddItem 0
cbDrive.AddItem 1
cbDrive.AddItem 2
cbDrive.AddItem 3
cbDrive.ListIndex = 0
End Sub
strResult += 磁盤類型: System.Convert.ToInt16(disk(DriveType).ToString())End IfMsgBox(strResult)NextEnd Sub總結:在VB.NET中,用API函數可以獲取硬盤信息。原來熟悉API函數VB6程序員,可以對API函數聲明進行適當的更改后,進行調用。利用FSO(文件系統(tǒng)對象)的Scrrun.DLL,也可以獲得磁盤信息。在.net Framwork中,利用WMI可以獲取更多的關于機器硬件的詳細信息(參考System.Management命名空間)。
樓上的朋友可能有點小小的誤會樓主的意思了,
樓主朋友可能要現在已經分好區(qū)的空間大小,已用空間、剩余空間。
當然我也不敢保證誰對誰錯,
我還是把我的理解 然后 也把代碼貼出來讓樓主看看吧
下面代碼的功能:顯示光驅當前分區(qū),以及各個盤的總空間,剩余空間。
當然。如果要硬盤總空間,我們可以把所有空間加起來,就達到要求了。
希望下面的代碼對樓主有用!
'硬盤空間大小 以及光驅
'添加Drive1 Label1 Label2
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Const DRIVE_CDROM = 5
Public drivenm As String, cddrive As String
Private Sub Form_Load()
'查找CD-ROM的驅動器號
cddrive = ""
For i = 65 To 90
If GetDriveType(Chr$(i) ":\") = DRIVE_CDROM Then
cddrive = UCase(Chr$(i)) ":\"
Exit For
End If
Next i
drivenm = "c:"
Label1.AutoSize = True
Label2.AutoSize = True
Drive1.Left = (Me.Width - Drive1.Width) \ 2
Drive1.Drive = "c"
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
gethd
End Sub
Private Sub Form_Activate()
MsgBox "你的光驅在:" cddrive
End Sub
Private Sub Drive1_Change()
drivenm = Mid(Drive1.Drive, 1, 3)
gethd
End Sub
Private Sub gethd() '得知硬盤容量
On Error Resume Next
Dim dfs, cl1, cl2, sec1, byt1, tspace, getdiskvolm, lSize, kk%
Dim hdtype$, hdspace$, hdfspace$
dfs = GetDiskFreeSpace(drivenm, sec1, byt1, cl1, cl2)
If dfs Then
cl2 = Int(cl2 * sec1 / 1024 * byt1)
lSize = Len(Format$(cl2, "#########"))
If lSize 11 Then
kk = 11 - lSize
End If
hdspace = Space(kk) + Format$(cl2, "#########") + " KBytes"
cl1 = Int(cl1 * sec1 / 1024 * byt1)
lSize = Len(Format$(cl1, "#########"))
If lSize 11 Then
kk = 11 - lSize
End If
hdfspace = Space(kk) + Format$(cl1, "#########") + " KBytes"
Else
hdspace = ""
hdfspace = ""
End If
Label1.Caption = "你的" drivenm "盤的總空間是:" Format(Str(Val(hdspace) / 1024 / 1024), "##0.0") + " G"
Label2.Caption = "你的" drivenm "盤剩余空間是:" Format(Str(Val(hdfspace) / 1024), "###,##0.0") + " M"
If UCase(Left(Drive1.Drive, 2)) = UCase(Left(cddrive, 2)) Then
If Val(Label1.Caption) = 0 And Val(Label2.Caption) = 0 Then
MsgBox "這張盤是空的光盤"
Else
If Val(Label1.Caption) 0 And Val(Label2.Caption) 0 Then
MsgBox "這張盤不是空的光盤,但還有空間"
Else
If Val(Label1.Caption) 0 And Val(Label2.Caption) = 0 Then
MsgBox "這張盤是寫滿并終止的光盤"
End If
End If
End If
End If
End Sub