vb.net 教程 4-2 目录操作 DirectoryInfo 4

前端之家收集整理的这篇文章主要介绍了vb.net 教程 4-2 目录操作 DirectoryInfo 4前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本节谈谈如何获得某个目录下的子目录和文件
当然,等到讲完了目录操作后,我将会举个类似于资源管理器的例子。

窗体设计界面如下:

列出子目录:
  1. Private Sub btnSubFolders_Click(sender As Object,e As EventArgs) Handles btnSubFolders.Click
  2. Dim di As New DirectoryInfo("c:\windows")
  3. If di.Exists = False Then Exit Sub
  4. txtFolderInfo.Text = ""
  5. For Each folderinfo As DirectoryInfo In di.GetDirectories
  6. txtFolderInfo.Text &= folderinfo.Name & ControlChars.CrLf
  7. Next
  8. End Sub
为简化操作,直接使用的Windows目录。

列出目录下的文件
  1. Private Sub btnFiles_Click(sender As Object,e As EventArgs) Handles btnFiles.Click
  2. Dim di As New DirectoryInfo("c:\windows")
  3. If di.Exists = False Then Exit Sub
  4. txtFolderInfo.Text = ""
  5. For Each finfo As FileInfo In di.GetFiles
  6. txtFolderInfo.Text &= finfo.Name & ControlChars.CrLf
  7. Next
  8. End Sub
提前用了点FileInfo的知识。

列出所有:
  1. Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
  2. Dim di As New DirectoryInfo("c:\windows")
  3. If di.Exists = False Then Exit Sub
  4.  
  5. Dim fsType As String
  6. txtFolderInfo.Text = ""
  7. For Each fsinfo As FileSystemInfo In di.GetFileSystemInfos
  8. If (fsinfo.Attributes And FileAttribute.Directory) = FileAttribute.Directory Then fsType = "目录" Else fsType = "文件"
  9. txtFolderInfo.Text &= fsType & " " & fsinfo.Name & ControlChars.CrLf
  10. Next
  11. End Sub
其实,DirectoryInfo和FileInfo都是继承于FileSystemInfo,很多属性方法都来自FileSystemInfo。
运行时如图:

学习更多vb.net知识,请参看 vb.net 教程 目录

猜你在找的VB相关文章