System.IO命名空间中Directory类的使用

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Directory.CreateDirectory("c:\dyc") '创建目录

If Directory.Exists("c:\dyc") Then
Directory.Delete("c:\dyc") '删除目录
End If

MsgBox(Directory.GetCurrentDirectory) '当前应用程序目录
MsgBox(Directory.GetDirectoryRoot("c:\windows\system32")) '根目录
'MsgBox(Directory.GetParent("c:\windows\system32")) '父目录
MsgBox(Directory.GetCreationTime("c:\windows\system32")) '目录创建的时间、时间

REM 查找子目录
Dim dirs() As String = Directory.GetDirectories("c:\windows")
For i = 0 To dirs.Length - 1
Debug.Print(dirs(i))
Next

REM 查找文件
Dim files() As String = Directory.GetFiles("c:\windows")
For i = 0 To files.Length - 1
Debug.Print(files(i))
Next


REM 查找子目录和文件
Dim dirfiles() As String = Directory.GetFileSystemEntries("c:\windows")
For i = 0 To dirfiles.Length - 1
Debug.Print(dirfiles(i))
Next

End Sub
End Class

精典使用案例:

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dirs() As String = Directory.GetDirectories("c:\")
Dim files() As String = Directory.GetFiles("c:\")
Dim dirfiles() As String = Directory.GetFileSystemEntries("c:\")
Dim dris() As String = Directory.GetLogicalDrives()


For Each temp As String In dirs
ListBox1.Items.Add(temp)
Next


For Each temp As String In files
ListBox2.Items.Add(temp)

Next

For Each temp As String In dirfiles
ListBox3.Items.Add(temp)

Next

For Each temp As String In dris
ListBox4.Items.Add(temp)

Next
End Sub
End Class

你可能感兴趣的:(Directory)