文件夹这个概念大家都很熟悉,在各各操作系统中都有文件夹这个概念,而在VB.NET这门开发语言中如何更好更安全的操作文件夹,这就是今天我们要来演示的一个案例。希望从VB.NET文件夹操作这个案例中学到技巧。
VB.NET文件夹操作代码:
'文件夹复制
Function CopyDir()Function CopyDir(ByVal sourcePath As String, ByVal targetPath As String) As Boolean
Try
'检查目标目录是否以目录分割字符结束,不是则添加
If Right(targetPath, 1) <> "" Then targetPath += ""
'判断目标目录是否存在,不存在则新建
If Not Directory.Exists(targetPath) Then Directory.CreateDirectory(targetPath)
' 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
Dim fileList As String() = Directory.GetFileSystemEntries(sourcePath)
'遍历所有的文件和目录
For Each filepath As String In fileList
'目录处理,递归
If (Directory.Exists(filepath)) Then
CopyDir(filepath, targetPath + Path.GetFileName(filepath))
Else
'复制文件
File.Copy(filepath, targetPath + Path.GetFileName(filepath), True)
End If
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
'文件夹删除
Function DelDir()Function DelDir(ByVal targetPath As String) As Boolean
Try
'检查目标目录是否以目录分割字符结束,不是则添加
If Right(targetPath, 1) <> "" Then targetPath += ""
'得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
Dim fileList As String() = Directory.GetFileSystemEntries(targetPath)
'遍历所有的文件和目录
For Each filepath As String In fileList
'目录处理,递归
If (Directory.Exists(filepath)) Then
DelDir(targetPath + Path.GetFileName(filepath))
Else
'删除文件
File.Delete(targetPath + Path.GetFileName(filepath))
End If
Next
'删除文件夹
System.IO.Directory.Delete(targetPath, True)
Return True
Catch ex As Exception
Return False
End Try
End Function
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
以上就是我为大家提高的关于VB.NET文件夹操作的一个案例,大家快试试吧!
【编辑推荐】