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

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

文件夹这个概念大家都很熟悉,在各各操作系统中都有文件夹这个概念,而在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文件夹操作的一个案例,大家快试试吧!

【编辑推荐】

  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 08:56:22

VB.NET文件夹

2010-01-12 09:51:07

VB.NET操作dbf

2009-10-27 17:59:16

VB.NET删除文件夹

2010-01-11 14:28:14

VB.NET操作Exc

2009-11-02 12:35:10

VB.NET追加文件

2009-10-27 16:05:52

VB.NET File

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-11-02 15:49:23

VB.NET显示系统信

2009-10-21 09:40:23

VB.NET搜索

2009-11-02 11:02:58

VB.NET XML文

2009-11-10 11:30:12

VB.NET循环语句

2009-10-28 13:24:25

VB.NET文件

2010-01-15 19:04:09

2009-10-29 15:28:38

VB.NET文件操作

2009-10-09 16:11:33

VB.NET语法

2009-11-10 15:18:35

VB.NET封面
点赞
收藏

51CTO技术栈公众号