vb.net 教程 4-4 目录操作 Directory 2
发布时间:2020-12-17 07:31:51 所属栏目:百科 来源:网络整理
导读:紧接上一节内容。 创建文件夹: 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"
紧接上一节内容。
创建文件夹:
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 教程 目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |