C#編寫自動差異備份文件夾
阿新 • • 發佈:2018-03-23
http rod fault 對話 object ted int32 status auto
手動或定時將源文件夾中的全部文件自動與目標文件夾中的文件做對比,如果源文件夾中的文件更新,則將其復制到目標文件夾中替換目標文件夾中的文件。
界面:
界面中的內容:
序號 |
控件類型 |
名稱 |
作用 |
1 |
TextBox |
tbxSourcePath |
源文件夾路徑 |
2 |
TextBox |
tbxDestPath |
目標文件夾路徑 |
3 |
NumericUpDown |
numTimer |
定時備份時間 |
4 |
ComboBox |
cbbCycle |
備份時間單位 |
5 |
CheckBox |
ckbAuto |
是否開啟定時備份 |
6 |
Button |
btnSourceSelect |
選擇源文件夾 |
7 |
Button |
btnDestSelect |
選擇目標文件夾 |
8 |
Button |
btnBackups |
開始備份 |
9 |
statusStrip |
toolStripStatusLabel1 |
備份是否成功信息 |
10 |
statusStrip |
toolStripStatusLabel3 |
當前時間 |
代碼:
窗口初始化:
1 private void Form1_Load(object sender, EventArgs e)2 { 3 //狀態條初始化 4 toolStripStatusLabel1.Text = ""; 5 toolStripStatusLabel2.Text = ""; 6 toolStripStatusLabel3.Text = DateTime.Now.ToString("HH: mm:ss"); 7 //固定窗口大小 8 this.MaximizeBox = false; 9 this.MaximumSize = this.Size; 10 this.MinimumSize = this.Size; 11 12 ckbAuto.Checked = false;//自動備份復選框默認不選擇 13 numTimer.Value = 10;//設置默認定時時間為10分鐘 14 tbxSourcePath.Text = "";//初始化源路徑 15 tbxDestPath.Text = "";//初始化目標路徑 16 //初始化下拉列表控件 17 cbbCycle.Items.Clear(); 18 cbbCycle.Items.Add("分鐘"); 19 cbbCycle.Items.Add("小時"); 20 cbbCycle.Items.Add("天"); 21 cbbCycle.SelectedIndex = 0; 22 23 }
選擇源文件夾與目標文件夾路徑:
1 //選擇文件路徑 2 private string ChooseFolder() 3 { 4 string result = ""; 5 //選擇文件夾對話框 6 FolderBrowserDialog a = new FolderBrowserDialog(); 7 a.Description = "請選擇源文件夾路徑"; 8 a.RootFolder = Environment.SpecialFolder.MyComputer; 9 a.ShowNewFolderButton = true; 10 11 if (a.ShowDialog() == DialogResult.OK)//判斷是否選擇了路徑 12 { 13 result = a.SelectedPath; 14 } 15 else 16 { 17 result = ""; 18 } 19 20 return result; 21 } 22 23 private void btnSourceSelect_Click(object sender, EventArgs e) 24 { 25 tbxSourcePath.Text = ChooseFolder(); 26 } 27 28 private void btnObjectSelect_Click(object sender, EventArgs e) 29 { 30 tbxDestPath.Text = ChooseFolder(); 31 }
備份文件函數:
1 //備份 2 private bool Backups() 3 { 4 bool result = true; 5 working = true; 6 //判斷是否選擇了文件夾 7 if (tbxSourcePath.Text.Trim() == "" || tbxDestPath.Text.Trim() == "") 8 { 9 result = false; 10 MessageBox.Show("未選擇文件夾路徑!","錯誤提示"); 11 return result; 12 } 13 //判斷文件夾是否存在 14 if (!Directory.Exists(tbxSourcePath.Text) || !Directory.Exists(tbxDestPath.Text)) 15 { 16 result = false; 17 MessageBox.Show("文件夾路徑非法!", "錯誤提示"); 18 return result; 19 } 20 //獲取路徑下的所有文件夾與文件 21 string[] sourceDirectorys = Directory.GetDirectories(tbxSourcePath.Text); 22 string[] destDirectorys = Directory.GetDirectories(tbxDestPath.Text); 23 string[] sourceFiles = Directory.GetFiles(tbxSourcePath.Text); 24 string[] destFiles = Directory.GetFiles(tbxDestPath.Text); 25 26 if(!BackupFiles(sourceFiles, destFiles, tbxDestPath.Text)) result = false; 27 if(!BackupDirectory(sourceDirectorys, destDirectorys, tbxDestPath.Text)) result = false; 28 29 working = false; 30 return result; 31 } 32 33 //備份文件 34 private bool BackupFiles(string[] sourceFiles, string[] destFiles,string destPath) 35 { 36 bool result = true; 37 try 38 { 39 for (int i = 0; i < sourceFiles.Length; i++) 40 { 41 bool shouldCopy = true; 42 //獲取源文件的名稱含後綴名 43 string sourcename1 = NiceFileProduce.DecomposePathAndName(sourceFiles[i],NiceFileProduce.DecomposePathEnum.NameOnly); 44 string sourcename2 = NiceFileProduce.DecomposePathAndName(sourceFiles[i], NiceFileProduce.DecomposePathEnum.ExtensionOnly).ToLower(); 45 string sourcename = sourcename1 + sourcename2; 46 47 DateTime ta = File.GetLastWriteTime(sourceFiles[i]);//獲取源文件左後寫入時間 48 49 for (int j = 0; j < destFiles.Length; j++) 50 { 51 //獲取目標路徑文件的名稱含後綴名 52 string destname1 = NiceFileProduce.DecomposePathAndName(destFiles[j], NiceFileProduce.DecomposePathEnum.NameOnly); 53 string destname2 = NiceFileProduce.DecomposePathAndName(destFiles[j], NiceFileProduce.DecomposePathEnum.ExtensionOnly).ToLower(); 54 string destname = destname1 + destname2; 55 if (sourcename == destname) 56 { 57 DateTime tb = File.GetLastWriteTime(destFiles[j]);//獲取目標文件左後寫入時間 58 if (DateTime.Compare(ta, tb) <= 0)//判斷源文件與目標文件那個更新修改 59 { 60 shouldCopy = false; 61 } 62 } 63 } 64 //拷貝文件 65 if (shouldCopy) 66 { 67 string destpath = destPath + @"\" + sourcename; 68 if (File.Exists(destpath)) 69 { 70 File.Delete(destpath); 71 } 72 File.Copy(sourceFiles[i], destpath); 73 } 74 } 75 } 76 catch(Exception ex) 77 { 78 result = false; 79 MessageBox.Show("拷貝文件失敗:"+ex.Message,"錯誤提示"); 80 } 81 return result; 82 } 83 84 //備份文件夾 85 private bool BackupDirectory(string[] sourceDirectorys, string[] destDirectorys, string destPath) 86 { 87 bool result = true; 88 try 89 { 90 for (int i = 0; i < sourceDirectorys.Length; i++) 91 { 92 string sourcename = NiceFileProduce.DecomposePathAndName(sourceDirectorys[i], NiceFileProduce.DecomposePathEnum.NameAndExtension); 93 string destpath = destPath + @"\" + sourcename; 94 if (!Directory.Exists(destpath)) 95 { 96 Directory.CreateDirectory(destpath); 97 } 98 99 //獲取路徑下的所有文件夾與文件 100 string[] sourceDirectorys2 = Directory.GetDirectories(sourceDirectorys[i]); 101 string[] destDirectorys2 = Directory.GetDirectories(destpath); 102 string[] sourceFiles = Directory.GetFiles(sourceDirectorys[i]); 103 string[] destFiles = Directory.GetFiles(destpath); 104 105 if (!BackupFiles(sourceFiles, destFiles, destpath)) result = false; 106 107 if (sourceDirectorys2.Length != 0) 108 { 109 if(!BackupDirectory(sourceDirectorys2, destDirectorys2, destpath)) result = false; 110 } 111 } 112 } 113 catch(Exception ex) 114 { 115 result = false; 116 MessageBox.Show("拷貝文件夾失敗:"+ex.Message,"錯誤提示"); 117 } 118 return result; 119 }
手動備份按鈕:
1 private void btnBackups_Click(object sender, EventArgs e) 2 { 3 if (working) 4 { 5 toolStripStatusLabel1.Text = "正在備份中,稍後再試"; 6 } 7 else 8 { 9 toolStripStatusLabel1.Text = "備份開始"; 10 if (Backups()) 11 { 12 toolStripStatusLabel1.Text = "手動備份成功" + DateTime.Now.ToString(" yyyy年mm月dd日 HH:mm:ss"); 13 } 14 else 15 { 16 toolStripStatusLabel1.Text = "手動備份失敗" + DateTime.Now.ToString(" yyyy年mm月dd日 HH:mm:ss"); 17 } 18 } 19 }
自動備份定時器設置:
1 private void ckbAuto_CheckedChanged(object sender, EventArgs e) 2 { 3 int time = 600000; 4 if (ckbAuto.Checked) 5 { 6 if (cbbCycle.Text == "分鐘") 7 { 8 time = Convert.ToInt32(numTimer.Value) * 1000 * 60; 9 } 10 else if (cbbCycle.Text == "小時") 11 { 12 time = Convert.ToInt32(numTimer.Value) * 1000 * 60 * 60; 13 } 14 else if (cbbCycle.Text == "天") 15 { 16 time = Convert.ToInt32(numTimer.Value) * 1000 * 60 * 60 * 24; 17 } 18 cbbCycle.Enabled = false; 19 numTimer.Enabled = false; 20 timer1.Interval = time; 21 timer1.Start(); 22 } 23 else 24 { 25 timer1.Stop(); 26 cbbCycle.Enabled = true; 27 numTimer.Enabled = true; 28 } 29 }
自動備份定時器:
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 if (working) 4 { 5 toolStripStatusLabel1.Text = "正在備份中,稍後再試"; 6 } 7 else 8 { 9 toolStripStatusLabel1.Text = "備份開始"; 10 if (Backups()) 11 { 12 toolStripStatusLabel1.Text = "定時備份成功" + DateTime.Now.ToString(" yyyy年mm月dd日 HH:mm:ss"); 13 } 14 else 15 { 16 toolStripStatusLabel1.Text = "定時備份失敗" + DateTime.Now.ToString(" yyyy年mm月dd日 HH:mm:ss"); 17 } 18 } 19 }
當前時間顯示:
1 private void timer2_Tick(object sender, EventArgs e) 2 { 3 toolStripStatusLabel3.Text = DateTime.Now.ToString("HH: mm:ss"); 4 }
分解文件路徑函數:
1 public class NiceFileProduce 2 { 3 //分解路徑用枚舉 4 public enum DecomposePathEnum 5 { 6 PathOnly = 0,//僅返回路徑 7 NameAndExtension = 1,//返回文件名+擴展名 8 NameOnly = 2,//僅返回文件名 9 ExtensionOnly = 3,//僅返回擴展名(帶.) 10 11 } 12 13 //------------【函數:將文件路徑分解】------------ 14 15 //filePath文件路徑 16 //DecomposePathEnum返回類型 17 //------------------------------------------------ 18 public static string DecomposePathAndName(string filePath, DecomposePathEnum decomposePathEnum) 19 { 20 string result = ""; 21 switch (decomposePathEnum) 22 { 23 case DecomposePathEnum.PathOnly://僅返回路徑 24 if (filePath.LastIndexOf("\\") < 0) 25 { 26 result = filePath; 27 } 28 else 29 { 30 result = filePath.Substring(0, filePath.LastIndexOf("\\")); 31 } 32 break; 33 case DecomposePathEnum.NameAndExtension://返回文件名+擴展名 34 if (filePath.LastIndexOf("\\") < 0) 35 { 36 result = filePath; 37 } 38 else 39 { 40 result = filePath.Substring(filePath.LastIndexOf("\\") + 1); 41 } 42 break; 43 case DecomposePathEnum.NameOnly://僅返回文件名 44 if (filePath.LastIndexOf("\\") < 0) 45 { 46 if (filePath.LastIndexOf(".") < 0) 47 { 48 result = filePath; 49 } 50 else 51 { 52 result = filePath.Substring(0, filePath.LastIndexOf(".") - filePath.LastIndexOf("\\") - 1); 53 } 54 } 55 else 56 { 57 if (filePath.LastIndexOf(".") < 0) 58 { 59 result = filePath.Substring(filePath.LastIndexOf("\\") + 1); 60 } 61 else 62 { 63 result = filePath.Substring(filePath.LastIndexOf("\\") + 1, filePath.LastIndexOf(".") - filePath.LastIndexOf("\\") - 1); 64 } 65 } 66 break; 67 case DecomposePathEnum.ExtensionOnly://僅返回擴展名(帶.) 68 if (filePath.LastIndexOf(".") < 0) 69 { 70 result = filePath; 71 } 72 else 73 { 74 result = filePath.Substring(filePath.LastIndexOf(".")); 75 } 76 break; 77 default:// 78 result = ""; 79 break; 80 } 81 return result; 82 } 83 }
C#編寫自動差異備份文件夾