Imports System Imports System.IO Namespace MyFileManagerNamespace MyFileManager Public Class FileManageClass FileManage Private Const strPath As String = "/" Private Sub New()Sub New() End Sub Public Shared Sub CopyFiles()Sub CopyFiles(ByVal sourceDir As String, ByVal targetDir As String, ByVal overWrite As Boolean) CopyFiles(sourceDir, targetDir, overWrite, False) '拷贝文件夹下的文件,不拷贝目录或拷贝单个文件 End Sub Public Shared Sub CopyFiles()Sub CopyFiles(ByVal sourceDir As String, ByVal targetDir As String, ByVal overWrite As Boolean, ByVal copySubDir As Boolean) For Each sourceFileName As String In Directory.GetFiles(sourceDir) '在这里要拷贝文件夹内的所有文件 Dim targetFileName As String = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(strPath) + 1)) '这里合并两个路径,以得到目标文件名 If File.Exists(targetFileName) Then If overWrite = True Then File.SetAttributes(targetFileName, FileAttributes.Normal) File.Copy(sourceFileName, targetFileName, overWrite) '覆盖拷贝文件 End If Else File.Copy(sourceFileName, targetFileName, overWrite) '拷贝文件 End If Next If copySubDir Then For Each sourceSubDir As String In Directory.GetDirectories(sourceDir) Dim targetSubDir As String = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(strPath) + 1)) '这里合并两个路径,以得到目标文件目录 If Not Directory.Exists(targetSubDir) Then Directory.CreateDirectory(targetSubDir) '如不存在,则创建 End If CopyFiles(sourceSubDir, targetSubDir, overWrite, True) '这里是递归调用,以下一样 '拷贝目录 Next End If End Sub Public Shared Sub MoveFiles()Sub MoveFiles(ByVal sourceDir As String, ByVal targetDir As String, ByVal overWrite As Boolean) MoveFiles(sourceDir, targetDir, overWrite, False) '移动文件夹下的文件或单个文件 End Sub Public Shared Sub MoveFiles()Sub MoveFiles(ByVal sourceDir As String, ByVal targetDir As String, ByVal overWrite As Boolean, ByVal moveSubDir As Boolean) For Each sourceFileName As String In Directory.GetFiles(sourceDir) Dim targetFileName As String = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf(strPath) + 1)) If File.Exists(targetFileName) Then If overWrite = True Then File.SetAttributes(targetFileName, FileAttributes.Normal) File.Delete(targetFileName) File.Move(sourceFileName, targetFileName) End If Else File.Move(sourceFileName, targetFileName) End If Next '移除文件夹下的所有文件(不包括目录) If moveSubDir Then For Each sourceSubDir As String In Directory.GetDirectories(sourceDir) Dim targetSubDir As String = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf(strPath) + 1)) If Not Directory.Exists(targetSubDir) Then Directory.CreateDirectory(targetSubDir) End If MoveFiles(sourceSubDir, targetSubDir, overWrite, True) '通过递归调用movefiles,把所有的文件move Directory.Delete(sourceSubDir) Next End If End Sub Public Shared Sub DeleteFiles()Sub DeleteFiles(ByVal targetDir As String) DeleteFiles(targetDir, False) '删除文件 End Sub Public Shared Sub DeleteFiles()Sub DeleteFiles(ByVal targetDir As String, ByVal delSubDir As Boolean) '删除所有文件及其目录 For Each fileName As String In Directory.GetFiles(targetDir) File.SetAttributes(fileName, FileAttributes.Normal) File.Delete(fileName) Next If delSubDir Then Dim dir As DirectoryInfo = New DirectoryInfo(targetDir) For Each subDi As DirectoryInfo In dir.GetDirectories '这个写法方便一点 DeleteFiles(subDi.FullName, True) subDi.Delete() Next End If End Sub Public Shared Sub CreateDirectory()Sub CreateDirectory(ByVal targetDir As String) ’创建目录 Dim dir As DirectoryInfo = New DirectoryInfo(targetDir) If Not dir.Exists Then dir.Create() End If End Sub Public Shared Sub CreateDirectory()Sub CreateDirectory(ByVal parentDir As String, ByVal subDirName As String) CreateDirectory(parentDir + strPath + subDirName) End Sub Public Shared Sub DeleteDirectory()Sub DeleteDirectory(ByVal targetDir As String) '删除目录及目录的内容 Dim dirInfo As DirectoryInfo = New DirectoryInfo(targetDir) If dirInfo.Exists Then DeleteFiles(targetDir, True) dirInfo.Delete(True) End If End Sub End Class End Namespace