vb.net获取磁盘信息 vbnet读取文件内容

VB.NET 中使用 combobox 控件获取磁盘信息

先引用 System.ManageMent

10年的峄城网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整峄城建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“峄城网站设计”,“峄城网站推广”以来,每个客户项目都认真落实执行。

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim HDDDeviceQuery As New Management.SelectQuery("Win32_DiskDrive")

Dim HDDDeviceSearch As New Management.ManagementObjectSearcher(HDDDeviceQuery)

Dim HDDDeviceInfo As Management.ManagementObject

For Each HDDDeviceInfo In HDDDeviceSearch.Get()

ComboBox1.Items.Add("HDD Description: " HDDDeviceInfo("caption").ToString())

ComboBox1.Items.Add("HDD BytesPerSector: " HDDDeviceInfo("BytesPerSector").ToString())

'ComboBox1.Items.Add("HDD CompressionMethod: " HDDDeviceInfo("CompressionMethod").ToString())

ComboBox1.Items.Add("HDD Index: " HDDDeviceInfo("Index").ToString())

'ComboBox1.Items.Add("HDD InstallDate: " HDDDeviceInfo("InstallDate").ToString())

ComboBox1.Items.Add("HDD Manufacturer: " HDDDeviceInfo("Manufacturer").ToString())

ComboBox1.Items.Add("HDD Partitions: " HDDDeviceInfo("Partitions").ToString() Space(1) GetLastDev())

ComboBox1.Items.Add("HDD Size: " Int(Val(HDDDeviceInfo("Size").ToString()) / 2 ^ 30) " GBytes")

ComboBox1.Items.Add("HDD TotalCylinders: " HDDDeviceInfo("TotalCylinders").ToString())

ComboBox1.Items.Add("HDD TotalSectors: " HDDDeviceInfo("TotalSectors").ToString())

ComboBox1.Items.Add("HDD TracksPerCylinder: " HDDDeviceInfo("TracksPerCylinder").ToString())

ComboBox1.Items.Add("HDD TotalHeads: " HDDDeviceInfo("TotalHeads").ToString())

ComboBox1.Items.Add("HDD TotalTracks: " HDDDeviceInfo("TotalTracks").ToString())

ComboBox1.Items.Add("HDD SectorsPerTrack: " HDDDeviceInfo("SectorsPerTrack").ToString())

ComboBox1.Items.Add("HDD SCSILogicalUnit: " HDDDeviceInfo("SCSILogicalUnit").ToString())

Next

End Sub

Public Function GetLastDev() As String

GetLastDev = ""

Dim r As Short = My.Computer.FileSystem.Drives.Count

For i As Short = 0 To r - 1 Step 1

GetLastDev = GetLastDev My.Computer.FileSystem.Drives(i).Name ":"

Next

Return GetLastDev

End Function

End Class

VB6中如何获取磁盘信息?

楼上的朋友可能有点小小的误会楼主的意思了,

楼主朋友可能要现在已经分好区的空间大小,已用空间、剩余空间。

当然我也不敢保证谁对谁错,

我还是把我的理解 然后 也把代码贴出来让楼主看看吧

下面代码的功能:显示光驱当前分区,以及各个盘的总空间,剩余空间。

当然。如果要硬盘总空间,我们可以把所有空间加起来,就达到要求了。

希望下面的代码对楼主有用!

'硬盘空间大小 以及光驱

'添加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

vb.net 磁盘文件列表,界面如图,在.net下如何实现?

预先准备三个图标文件,用于树型控件中显示磁盘符号和文件夹的图像之用。

1、窗体上添加控件如下:

组合框控件 ComboBox1,树型控件 TreeView1,列表框控件 ListBox1,图像列表控件 ImageList1。

选中TreeView1,设置其ImageList属性为ImageList1。

2、设置属性

选中图像列表控件 ImageList1,在属性窗口里,选中属性Images,单击三个小点按钮,出现图像集合编辑器窗口,单击[添加按钮],一一把准备好的图标文件进行添加,注意先后次序,如果不符合要求可以通过上下移动按钮重新改变次序。完成后单击[确定]。

运行图如下:

完整代码如下:

Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'添加系统所有磁盘目录符号

For Each MyDrive As String In Environment.GetLogicalDrives()

ComboBox1.Items.Add(MyDrive)

Next

'显示第一个磁盘符号

ComboBox1.Text = ComboBox1.Items(0)

End Sub

'递归过程添加目录树

Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)

Dim i As Integer

Dim Mynode As New TreeNode

'先添加本目录

Mynode.Text = Strings.Replace(strPath, strFatherPath  "\", "", , 1)

'为节点指定未被选中时显示的图标

Mynode.ImageIndex = 1

'为节点指定被选中时显示的图标

Mynode.SelectedImageIndex = 2

nodeFather.Nodes.Add(Mynode)

Application.DoEvents()

Try

Dim str() As String = Directory.GetDirectories(strPath)

'递归遍历该目录的子文件夹

For i = 0 To str.GetUpperBound(0)

AddDirectory(strPath, str(i), Mynode)

Next

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

Mynode = Nothing

End Sub

'根据给出的盘符添加目录树

Private Sub AddRootDirectory(ByVal DiscSymbol As String)

Dim Nynode As New TreeNode

'先把磁盘盘符添加到树中

TreeView1.Nodes.Clear()

Nynode.ImageIndex = 0

Nynode.Text = DiscSymbol

Nynode.SelectedImageIndex = -1

TreeView1.Nodes.Add(Nynode)

Dim i As Integer

'获取磁盘根目录下的文件夹

Dim str() As String = Directory.GetDirectories(DiscSymbol  "\")

For i = 0 To str.GetUpperBound(0)

'调用递归过程遍历该文件夹里的所有子文件夹,并添加到树型控件

AddDirectory(DiscSymbol, str(i), Nynode)

Next

Nynode = Nothing

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

'根据磁盘符号的变更,显示根目录里的文件

ListBox1.Items.Clear()

For Each MyFile As String In System.IO.Directory.GetFiles(ComboBox1.Text)

ListBox1.Items.Add(MyFile)

Next

'根据磁盘符号的变更,重新显示目录树

Dim DiscSymbol As String

DiscSymbol = Microsoft.VisualBasic.Left(ComboBox1.Text, Len(ComboBox1.Text) - 1)

Call AddRootDirectory(DiscSymbol)

End Sub

'递归过程根据子目录寻找上级目录名--从而构成完整的目录路径

Private Sub AllPath(ByVal ThisNode As TreeNode, ByRef MyPathName As String)

If ThisNode.Level  1 Then

'该节点层数大于1,其父节点不是磁盘根目录

MyPathName = ThisNode.Parent.Text  "\"  MyPathName

Dim MyNode As TreeNode = ThisNode.Parent

Call AllPath(MyNode, MyPathName)

Else

'该节点层数等于1,其父节点就是磁盘根目录

MyPathName = ComboBox1.Text  MyPathName

End If

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

'为了搜索选中的节点对应目录的文件,需要组成全路径

Dim MyAllPathName As String = TreeView1.SelectedNode.Text

Dim MyNode As TreeNode = TreeView1.SelectedNode

If TreeView1.SelectedNode.Level = 0 Then

'如果选中的是根节点

MyAllPathName = ComboBox1.Text

Else

'如果选中的是非根节点,调用递归过程组成全路径

Call AllPath(MyNode, MyAllPathName)

MyAllPathName = MyAllPathName  "\"

End If

'根据路径,搜索文件名并显示

ListBox1.Items.Clear()

For Each MyFile As String In System.IO.Directory.GetFiles(MyAllPathName)

ListBox1.Items.Add(MyFile)

Next

End Sub

End Class

vb获取硬件信息的代码

tmpstr=""

set fso=createObject("scripting.filesystemObject")

Set dc = fso.Drives

for each d in dc

If d.isReady Then

tmpstr=tmpstr "磁盘" d.DriveLetter ":" vbcrlf _

"可用空间:" (d.AvailableSpace/1024/1024/1024) " GB" vbcrlf _

"空余空间:" (d.FreeSpace/1024/1024/1024) " GB"  vbcrlf _

"总空间大小:" (d.TotalSize/1024/1024/1024) " GB"  vbcrlf _

"文件系统:" d.FileSystem vbcrlf _

"驱动器类型:" d.DriveType vbcrlf _

"是否就绪:" d.IsReady vbcrlf _

"路径:" d.Path vbcrlf _

"根目录:" d.RootFolder vbcrlf _

"序列号:" d.SerialNumber vbcrlf _

"共享名:" d.ShareName vbcrlf _

"卷名:" d.VolumeName vbcrlf vbcrlf

End If

Next

msgbox tmpstr


文章名称:vb.net获取磁盘信息 vbnet读取文件内容
路径分享:http://myzitong.com/article/hpseep.html