using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace mainProgram { public partial class SaveAsWindow : Form { private string mSource; public SaveAsWindow(string source) { InitializeComponent(); mSource = source; } private void SelectPathBtn_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件夹"; if (dialog.ShowDialog() == DialogResult.OK) { if (string.IsNullOrEmpty(dialog.SelectedPath)) { MessageBox.Show(this, "文件夹路径不能为空", "提示"); return; } string savePath = dialog.SelectedPath; PathTextBox.Text = savePath; } } private void CancelBtn_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; Close(); } private void OkBtn_Click(object sender, EventArgs e) { if (!Directory.Exists(PathTextBox.Text)) { Directory.CreateDirectory(PathTextBox.Text); } CopySubFun(mSource, PathTextBox.Text, true); Close(); } private void CopySubFun(string sourceName, string destFolderName, bool overwrite)//递归函数 { if (File.Exists(sourceName))//是文件,直接拷贝 { string sourceFileName = Path.GetFileName(sourceName);//获取文件名 File.Copy(sourceName, Path.Combine(destFolderName, sourceFileName), overwrite); } else if (Directory.Exists(sourceName))//是文件夹,拷贝文件夹;并递归 { string[] sourceFolders = sourceName.Split('\\'); string lastDirectory = sourceFolders[sourceFolders.Length - 1]; string dest = Path.Combine(destFolderName, lastDirectory); Directory.CreateDirectory(dest); string[] sourceFilesPath = Directory.GetFileSystemEntries(sourceName); for (int i = 0; i < sourceFilesPath.Length; i++) { string sourceFilePath = sourceFilesPath[i]; CopySubFun(sourceFilePath, dest, overwrite); } } } } }