因为有好多台服务器,每次都要手动进行备份,有点麻烦,而且容易出错,所以写了下面这个用程序来备份,比较简单,就是用C#复制文件和文件
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using System.Configuration; 6 7 namespace Copy 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //eg D:\publish 14 //不用 ConfigurationManager而用下面这个是因为 FrameWork2.0 15 string sourceDir = ConfigurationSettings.AppSettings["SourcePath"].ToString(); 16 //eg D:\BackUp 17 string targetDir = ConfigurationSettings.AppSettings["TargetPath"].ToString(); 18 Console.WriteLine("正在进行备份,请不要关闭窗口............."); 19 DirectoryCopy(sourceDir, targetDir); 20 Console.WriteLine("成功复制备份,按任意键关闭窗口............"); 21 Console.ReadKey(); 22 } 23 24 static void DirectoryCopy(string sourceDir, string targetDir) 25 { 26 if (!Directory.Exists(sourceDir)) 27 return; 28 if (!Directory.Exists(targetDir)) 29 return; 30 31 string sourceFolderName = sourceDir.Replace(Directory.GetParent(sourceDir).ToString(), "").Replace(Path.DirectorySeparatorChar.ToString(), ""); 32 33 if (sourceDir == targetDir + sourceFolderName) 34 return; 35 36 //要复制到的路径 37 string tagetPath=targetDir + Path.DirectorySeparatorChar.ToString() + sourceFolderName; 38 if (Directory.Exists(tagetPath)) 39 { 40 Directory.Delete(tagetPath, true); 41 } 42 43 Directory.CreateDirectory(tagetPath); 44 45 //复制文件 46 string[] files = Directory.GetFiles(sourceDir); 47 for (int i = 0; i < files.Length; i++) 48 { 49 File.Copy(files[i], tagetPath+Path.DirectorySeparatorChar.ToString()+Path.GetFileName(files[i])); 50 } 51 //复制目录 52 string[] dires = Directory.GetDirectories(sourceDir); 53 for (int j = 0; j < dires.Length; j++) 54 { 55 DirectoryCopy(dires[j], tagetPath); 56 } 57 } 58 } 59 }