教你更快速使用VB.NET文件夾操作
作者:佚名
本文主要就VB.NET文件夾操作進(jìn)行代碼詳細(xì)演示,讓你更輕松的運(yùn)用,代碼只要復(fù)雜粘貼到機(jī)器上就可以跑起來用,但是還是希望大家看懂下面的代碼。
文件夾這個(gè)概念大家都很熟悉,在各各操作系統(tǒng)中都有文件夾這個(gè)概念,而在VB.NET這門開發(fā)語言中如何更好更安全的操作文件夾,這就是今天我們要來演示的一個(gè)案例。希望從VB.NET文件夾操作這個(gè)案例中學(xué)到技巧。
VB.NET文件夾操作代碼:
- '文件夾復(fù)制
- Function CopyDir()Function CopyDir(ByVal sourcePath As String, ByVal targetPath As String) As Boolean
- Try
- '檢查目標(biāo)目錄是否以目錄分割字符結(jié)束,不是則添加
- If Right(targetPath, 1) <> "" Then targetPath += ""
- '判斷目標(biāo)目錄是否存在,不存在則新建
- If Not Directory.Exists(targetPath) Then Directory.CreateDirectory(targetPath)
- ' 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個(gè)數(shù)組
- 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
- '復(fù)制文件
- 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
- '檢查目標(biāo)目錄是否以目錄分割字符結(jié)束,不是則添加
- If Right(targetPath, 1) <> "" Then targetPath += ""
- '得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個(gè)數(shù)組
- 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
以上就是我為大家提高的關(guān)于VB.NET文件夾操作的一個(gè)案例,大家快試試吧!
【編輯推薦】
責(zé)任編輯:田樹
來源:
博客