vb.net磁盘目录列表 vb打开目录

vb.net如何获取电脑中的所有盘符首先使用 System.IO.DriveInfo.GetDrives()获取System.IO.DriveInfovb.net磁盘目录列表,存入ds()
然后遍历dsvb.net磁盘目录列表 , 获取各个信息部分 。
Dim ds() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
For i As Integer = 0 To ds.Length - 1
TextBox1.Text = TextBox1.Textds(i).DriveType.ToString" " '驱动器类型
TextBox1.Text = TextBox1.Textds(i).Name" " '盘符(驱动器名)
TextBox1.Text = TextBox1.Textds(i).IsReady.ToString" " '是否就绪
If ds(i).IsReady = True Then
TextBox1.Text = TextBox1.Textds(i).VolumeLabel" " '卷标
TextBox1.Text = TextBox1.Textds(i).TotalSize.ToString" " '驱动器容量
TextBox1.Text = TextBox1.Textds(i).TotalFreeSpace.ToString '驱动器可用容量
End If
TextBox1.Text = TextBox1.TextvbNewLine
Next
vb.net 磁盘文件列表,界面如图,在.net下如何实现?预先准备三个图标文件vb.net磁盘目录列表,用于树型控件中显示磁盘符号和文件夹的图像之用 。
1、窗体上添加控件如下vb.net磁盘目录列表:
组合框控件 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.Level1 Then
'该节点层数大于1,其父节点不是磁盘根目录
MyPathName = ThisNode.Parent.Text"\"MyPathName
Dim MyNode As TreeNode = ThisNode.Parent
Call AllPath(MyNode, MyPathName)
Else
'该节点层数等于1,其父节点就是磁盘根目录
MyPathName = ComboBox1.TextMyPathName
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.NET获取目录文件夹名称先用System.IO.Directory.GetDirectories函数获取子目录的名称(包括其路径),再用System.IO.Path.GetFileName获取子目录的名称 。下面是代码:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each s In System.IO.Directory.GetDirectories("C:\Windows")
Console.WriteLine(System.IO.Path.GetFileName(s))
Next
End Sub
下面是部分输出:
Application Data
AppPatch
assembly
BOCNET
Boot
Branding
ConfigSetRoot
Cursors
Debug
DigitalLocker
Downloaded Installations
Downloaded Program Files
ehome
en-US
Fonts
Globalization
Help
...
可能有更简洁的方法,你可以到MSDN看看
System.IO.Directory.GetDirectories:
System.IO.Path.GetFileName:
通用 I/O 任务:
VB.net列举文件夹的所有文件vb.net使用控件folderbrowserdialog1,在程序中:
'设置对话框中在树视图控件上显示的说明文本
me.folderbrowserdialog1.description
=
"请选择输出报表所在路径:"
'设置从其开始浏览的根文件夹
me.folderbrowserdialog1.selectedpath
=
"c:\"
if
me.folderbrowserdialog1.showdialog()
=
dialogresult.ok
then
'取得全路径(包含文件名)
reportpath1
=
system.io.path.getfullpath(me.folderbrowserdialog1.selectedpath)
'设定text显示文件名
txtreport1.text
=
reportpath1
setreportlist()
end
if
在setreportlist()中针对你所需要的文件进行操作等
【vb.net磁盘目录列表 vb打开目录】vb.net磁盘目录列表的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于vb打开目录、vb.net磁盘目录列表的信息别忘了在本站进行查找喔 。

    推荐阅读