紧接上一节内容。
创建文件夹:
Private Sub btnCreateFolder_Click(sender As Object,e As EventArgs) Handles btnCreateFolder.Click If Directory.Exists("d:\bb") Then txtFolderInfo.Text = "该文件夹已经存在" Else Directory.CreateDirectory("d:\bb") txtFolderInfo.Text = "创建文件夹成功" End If End Sub
Private Sub btnDelFolder_Click(sender As Object,e As EventArgs) Handles btnDelFolder.Click If Directory.Exists("d:\bb") Then Try Directory.Delete("d:\bb") txtFolderInfo.Text = "该文件夹已经删除" Catch ex As Exception txtFolderInfo.Text = "删除文件夹时发生错误:" & ex.Message End Try Else txtFolderInfo.Text = "该文件夹不存在" End If End Sub
Private Sub btnDelFolder2_Click(sender As Object,e As EventArgs) Handles btnDelFolder2.Click If Directory.Exists("d:\bb") Then Try Directory.Delete("d:\bb",True) txtFolderInfo.Text = "该文件夹已经删除" Catch ex As Exception txtFolderInfo.Text = "删除文件夹时发生错误:" & ex.Message End Try Else txtFolderInfo.Text = "该文件夹不存在" End If End Sub
移动文件夹,仍然是只能在同一磁盘分区下移动:
Private Sub btnMoveFolder_Click(sender As Object,e As EventArgs) Handles btnMoveFolder.Click If Directory.Exists("d:\bb") Then Try Directory.Move("d:\bb","d:\dd") txtFolderInfo.Text = "文件夹已经移动完毕" Catch ex As Exception txtFolderInfo.Text = "移动文件夹时发生错误," & ex.Message End Try Else txtFolderInfo.Text = "文件夹不存在" End If End Sub
列出子目录:
Private Sub btnSubFolders_Click(sender As Object,e As EventArgs) Handles btnSubFolders.Click Dim allFolders() As String allFolders = Directory.GetDirectories("c:\") txtFolderInfo.Text = "" For Each folder As String In allFolders txtFolderInfo.Text &= folder & ControlChars.CrLf Next End Sub
列出文件
Private Sub btnFiles_Click(sender As Object,e As EventArgs) Handles btnFiles.Click Dim allFiles() As String allFiles = Directory.GetFiles("c:\") txtFolderInfo.Text = "" For Each file As String In allFiles txtFolderInfo.Text &= file & ControlChars.CrLf Next End Sub
列出所有,好像不能直接区别是目录还是文件:
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim allFoldersFiles() As String allFoldersFiles = Directory.GetFileSystemEntries("c:\") txtFolderInfo.Text = "" For Each file As String In allFoldersFiles txtFolderInfo.Text &= file & ControlChars.CrLf Next End Sub
学习更多vb.net知识,请参看 vb.net 教程 目录