如何将整个文件夹及其内容复制到用户选择的位置? [英] How can I copy an entire folder and its contents to a user selected location?
问题描述
我正在尝试将与我正在运行的程序位于同一位置的整个文件夹及其所有内容复制到用户选择的位置.我从使用SaveFileDialog开始,但是现在我正在使用FolderBrowserDialog.这是一些代码:
私有 子 BackupButton_Click( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 BackupButton.点击
Dim sourcePath As String
sourcePath = Application.StartupPath& "
Dim destPath As String
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
如果 FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK 然后
destPath = FolderBrowserDialog1.SelectedPath
结束 如果
Dim dir As 新建 DirectoryInfo(sourcePath)
Dim 信息 As FileSystemInfo()= dir.GetFileSystemInfos
BackupFiles(信息)
结束 子
私有 sourcePath As 字符串
私有 destPath As 字符串
私有 子 BackupFiles( ByVal FSInfo() As FileSystemInfo)
Dim i As FileSystemInfo
对于 每个 i 在中
如果 TypeOf i 是 DirectoryInfo 然后
' 我是目录
Dim sourceDir As String = i.FullName .ToString
' 匹配dest-filespec
Dim destDir As String = Replace(sourceDir ,sourcePath,destPath)
' 如果备份目标"中的目录不存在,请复制包含子目录和文件的目录
如果 不是 My.Computer.FileSystem.DirectoryExists(destDir)然后
My.Application.DoEvents()
My.Computer.FileSystem.CopyDirectory(sourceDir,destDir,真)
继续 对于
其他
Dim dInfo As DirectoryInfo = CType (i ,DirectoryInfo)
' 再次为现有Dirs循环,以检查必须备份的新Subdir和文件
BackupFiles(dInfo.GetFileSystemInfos)
结束 如果
ElseIf TypeOf i 是 FileInfo 然后
Dim sourceFile As String = i.FullName .ToString
Dim destFile As String = Replace(sourceFile ,sourcePath,destPath)
如果 不是 File.Exists(destFile)然后
My.Application.DoEvents()
File.Copy(sourceFile,destFile,真)
其他
如果 File.GetLastWriteTime(sourceFile)> File.GetLastWriteTime(destFile)然后
My.Application.DoEvents()
File.Copy(sourceFile,destFile,真)
结束 如果
结束 如果
结束 如果
下一步
结束 子
但是,当我运行该程序时,它可以工作,但是它不能将整个文件夹复制到应用程序启动位置..有什么想法可以将与程序位于同一位置的文件夹复制到用户选择的位置吗? class ="h2_lin">解决方案
没关系.我又弄乱了三个小时,并成功地使它工作.
I am trying to copy an entire folder and all of its contents that is in the same location as my program that is running to a user selected location. I started off by using a SaveFileDialog, but now I am working with a FolderBrowserDialog. Here is some code:
Private Sub BackupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupButton.Click
Dim sourcePath As String
sourcePath = Application.StartupPath & "\SavedFiles\"
Dim destPath As String
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
destPath = FolderBrowserDialog1.SelectedPath
End If
Dim dir As New DirectoryInfo(sourcePath)
Dim infos As FileSystemInfo() = dir.GetFileSystemInfos
BackupFiles(infos)
End Sub
Private sourcePath As String
Private destPath As String
Private Sub BackupFiles(ByVal FSInfo() As FileSystemInfo)
Dim i As FileSystemInfo
For Each i In FSInfo
If TypeOf i Is DirectoryInfo Then
'i is a Directory
Dim sourceDir As String = i.FullName.ToString
'Matching dest-filespec
Dim destDir As String = Replace(sourceDir, sourcePath, destPath)
'If Dir in Backup-Destination not exists, copy Dir with SubDirs and Files
If Not My.Computer.FileSystem.DirectoryExists(destDir) Then
My.Application.DoEvents()
My.Computer.FileSystem.CopyDirectory(sourceDir, destDir, True)
Continue For
Else
Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)
'Start the loop again for existing Dirs to check for new Subdirs and Files that must be backuped
BackupFiles(dInfo.GetFileSystemInfos)
End If
ElseIf TypeOf i Is FileInfo Then
Dim sourceFile As String = i.FullName.ToString
Dim destFile As String = Replace(sourceFile, sourcePath, destPath)
If Not File.Exists(destFile) Then
My.Application.DoEvents()
File.Copy(sourceFile, destFile, True)
Else
If File.GetLastWriteTime(sourceFile) > File.GetLastWriteTime(destFile) Then
My.Application.DoEvents()
File.Copy(sourceFile, destFile, True)
End If
End If
End If
Next
End Sub
But when I run the program, it works, but it does not copy the entire folder in the application startup location.. Any ideas how I can copy that folder that is in the same location as my program to a user selected location?
Never mind. I messed around with it for another 3 hours and successfully got it working.
这篇关于如何将整个文件夹及其内容复制到用户选择的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!