教你更快速使用VB.NET文件夹操作

开发 后端
本文主要就VB.NET文件夹操作进行代码详细演示,让你更轻松的运用,代码只要复杂粘贴到机器上就可以跑起来用,但是还是希望大家看懂下面的代码。

文件夹这个概念大家都很熟悉,在各各操作系统中都有文件夹这个概念,而在VB.NET这门开发语言中如何更好更安全的操作文件夹,这就是今天我们要来演示的一个案例。希望从VB.NET文件夹操作这个案例中学到技巧。

VB.NET文件夹操作代码:

  1. '文件夹复制  
  2. Function CopyDir()Function CopyDir(ByVal sourcePath As String, ByVal targetPath As String) As Boolean  
  3. Try  
  4. '检查目标目录是否以目录分割字符结束,不是则添加  
  5. If Right(targetPath, 1) <> "" Then targetPath += ""  
  6. '判断目标目录是否存在,不存在则新建  
  7. If Not Directory.Exists(targetPath) Then Directory.CreateDirectory(targetPath)  
  8. ' 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组  
  9. Dim fileList As String() = Directory.GetFileSystemEntries(sourcePath)  
  10. '遍历所有的文件和目录  
  11. For Each filepath As String In fileList  
  12. '目录处理,递归  
  13. If (Directory.Exists(filepath)) Then  
  14. CopyDir(filepath, targetPath + Path.GetFileName(filepath))  
  15. Else  
  16. '复制文件  
  17. File.Copy(filepath, targetPath + Path.GetFileName(filepath), True)  
  18. End If  
  19. Next  
  20. Return True  
  21. Catch ex As Exception  
  22. Return False  
  23. End Try  
  24. End Function  
  25. '文件夹删除  
  26. Function DelDir()Function DelDir(ByVal targetPath As String) As Boolean  
  27. Try  
  28. '检查目标目录是否以目录分割字符结束,不是则添加  
  29. If Right(targetPath, 1) <> "" Then targetPath += ""  
  30. '得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组  
  31. Dim fileList As String() = Directory.GetFileSystemEntries(targetPath)  
  32. '遍历所有的文件和目录  
  33. For Each filepath As String In fileList  
  34. '目录处理,递归  
  35. If (Directory.Exists(filepath)) Then  
  36. DelDir(targetPath + Path.GetFileName(filepath))  
  37. Else  
  38. '删除文件  
  39. File.Delete(targetPath + Path.GetFileName(filepath))  
  40. End If  
  41. Next  
  42. '删除文件夹  
  43. System.IO.Directory.Delete(targetPath, True)  
  44. Return True  
  45. Catch ex As Exception  
  46. Return False  
  47. End Try  
  48. End Function 

以上就是我为大家提高的关于VB.NET文件夹操作的一个案例,大家快试试吧!

【编辑推荐】

  1. 实例讲述VB.NET使用Log4Net
  2. 三分钟学会VB.NET转换形态
  3. VB.NET获取硬盘信息四大法宝
  4. 讲述VB.NET调用Excel的好处
  5. 简单例子概述VB.NET新窗体
责任编辑:田树 来源: 博客
相关推荐

2010-01-13 10:25:30

VB.NET文件夹操作

2010-01-21 13:34:56

VB.NET删除文件夹

2009-10-27 17:59:16

VB.NET删除文件夹

2009-10-27 08:56:22

VB.NET文件夹

2010-01-12 09:51:07

VB.NET操作dbf

2009-11-02 12:35:10

VB.NET追加文件

2009-10-27 16:05:52

VB.NET File

2010-01-11 14:28:14

VB.NET操作Exc

2009-11-02 13:54:27

VB.NET shel

2009-10-29 14:16:32

VB.NET读写文本文

2009-11-02 17:54:44

VB.NET数组

2009-10-21 09:40:23

VB.NET搜索

2009-11-02 15:49:23

VB.NET显示系统信

2009-11-02 11:02:58

VB.NET XML文

2009-10-28 13:24:25

VB.NET文件

2009-10-29 15:28:38

VB.NET文件操作

2010-01-15 19:04:09

2009-10-09 16:11:33

VB.NET语法

2009-11-10 11:30:12

VB.NET循环语句

2009-11-10 15:18:35

VB.NET封面
点赞
收藏

51CTO技术栈公众号