扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
首先使用 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
Private Function 硬盘序列号() As String
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有效,不过好像在有些机器上没用。
楼上的朋友可能有点小小的误会楼主的意思了,
楼主朋友可能要现在已经分好区的空间大小,已用空间、剩余空间。
当然我也不敢保证谁对谁错,
我还是把我的理解 然后 也把代码贴出来让楼主看看吧
下面代码的功能:显示光驱当前分区,以及各个盘的总空间,剩余空间。
当然。如果要硬盘总空间,我们可以把所有空间加起来,就达到要求了。
希望下面的代码对楼主有用!
'硬盘空间大小 以及光驱
'添加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
strResult += 磁盘类型: System.Convert.ToInt16(disk(DriveType).ToString())End IfMsgBox(strResult)NextEnd Sub总结:在VB.NET中,用API函数可以获取硬盘信息。原来熟悉API函数VB6程序员,可以对API函数声明进行适当的更改后,进行调用。利用FSO(文件系统对象)的Scrrun.DLL,也可以获得磁盘信息。在.net Framwork中,利用WMI可以获取更多的关于机器硬件的详细信息(参考System.Management命名空间)。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流