c# - C# - 复制文件和文件夹检查是否存在

尝试在不创建目录的情况下复制文件,

  • 检查目标位置是否存在文件/文件夹,是否比源文件/文件夹旧,
  • 如果文件/文件夹不存在,复制它,如果文件/文件夹比源文件旧,复制它
  • 如果文件文件夹存在于目标位置而不是源(表明它被删除了)中,则将文件从目标位置移动到存档文件夹,

下面是我到目前为止所做的事情,它实现了一切,除了#3中描述的内容,


using System;


using System.Collections.Generic;


using System.IO;


using System.Linq;


using System.Text;


using System.Threading.Tasks;



namespace FileCopy


{


 class Program


 {


 public static string dtnow = DateTime.Now.ToString("mmdd_hhmm");


 public static string watch_path = string.Empty;


 public static string copy_path = string.Empty;


 public static string final_copy_path = string.Empty;


 public static string log_folder = @"C:copylogs";


 //public static string log_file ="copy_log";


 public static string log_file = Path.Combine(log_folder +"copy_log" + dtnow +".txt");


 public static int newer_count = 0;


 public static int skip_count = 0;


 public static int copy_count = 0;


 public static int totalcount = 0;


 public static int currentcount = 0;



 static void Main(string[] args)


 {


 if (args == null || args.Length < 2)


 {


 Console.WriteLine("Invalid Syntax");


 return;


 }


 else


 {


 watch_path = args[0];


 copy_path = args[1];


 log_start_Check(log_folder,log_file);


 CopyFolder(watch_path, copy_path);


 finalcount_statement();


 }



 }



// Log Folder check and creation


 public static void log_start_Check(string log_folder, string log_file)


 {


 Console.Write("Checking Log Folder:");


 if (!Directory.Exists(log_folder))


 {


 try


 {


 Directory.CreateDirectory(log_folder);


 Console.WriteLine("Created!");


 }


 catch (Exception error)


 {


 Console.WriteLine("Unable to Create Directory" + error);


 return;


 }


 }


 else


 {


 Console.WriteLine("Exists");


 }


 Console.Write("Checking Log File:");


 //Console.WriteLine(log_file);


 if (!File.Exists(log_file))


 {


 try


 {


 File.Create(log_file);


 Console.WriteLine("Created!");


 }


 catch (Exception error)


 {


 Console.WriteLine("Unable to Create file" + error);


 return;


 }


 }


 else


 {


 Console.WriteLine("Exists");


 }


 Console.WriteLine();


 }



// Copy Folder Functions


 static public void CopyFolder(string sourceFolder, string destFolder)


 {


 try


 {


 if (!Directory.Exists(destFolder))


 Directory.CreateDirectory(destFolder);


 totalcount = Directory.GetFiles(sourceFolder,"*.*", SearchOption.AllDirectories).Count();


 string[] files = Directory.GetFiles(sourceFolder);


 totalcount = files.Length;


 foreach (string file in files)


 {


 string name = Path.GetFileName(file);


 FileInfo source_info = new FileInfo(file);


 string dest = Path.Combine(destFolder, name);


 FileInfo dest_info = new FileInfo(dest);


 if (File.Exists(dest))


 {


 try


 {


 if (source_info.LastWriteTime > dest_info.LastWriteTime)


 {


 //Console.Write("r" + currentcount +" of" + totalcount +" Completed");


 Console.ResetColor();


 Console.ForegroundColor = ConsoleColor.Yellow;


 File.Copy(file, dest, true);


 Console.Write("rFile Newer, File Copied" + dest +"");


 Console.ResetColor();


 newer_count++;


 }


 else


 {


 //Console.Write("r" + currentcount +" of" + totalcount +" Completed");


 Console.ResetColor();


 Console.ForegroundColor = ConsoleColor.Red;


 Console.Write("r** - File Exists" + dest +"");


 Console.ResetColor();


 skip_count++;


 } 


 }


 catch (Exception error)


 {


 error_handling("Error in Application" + error.Message, dest, file);


 }


 }


 else


 {


 try


 {


 //Console.Write("r"+currentcount +" of" + totalcount +" Completed");


 Console.ResetColor();


 Console.ForegroundColor = ConsoleColor.Green;


 File.Copy(file, dest, false);


 Console.Write("rFile Copied" + dest +"");


 Console.ResetColor();


 copy_count++;


 }


 catch (Exception error)


 {


 error_handling("Error in Application" + error.Message, dest, file);


 }


 }


 currentcount++;


 }


 string[] folders = Directory.GetDirectories(sourceFolder);


 foreach (string folder in folders)


 {


 string name = Path.GetFileName(folder);


 string dest = Path.Combine(destFolder, name);


 CopyFolder(folder, dest);


 }


 }


 catch (Exception error)


 {


 error_handling("Error in Application" + error.Message, null, null);


 }


 }


// Error Handling to add messages to logs


 public static void error_handling(string message, string fileident, string source)


 {


 using (System.IO.StreamWriter myFile = new System.IO.StreamWriter(log_file, true))


 {


 string finalMessage = string.Format("{0}: {1} SOURCE: {3} - DEST: {2}", DateTime.Now, message, fileident, source, Environment.NewLine);


 myFile.WriteLine(finalMessage);


 myFile.Close();


 }


 }


 // Final Statement


 public static void finalcount_statement()


 {


 Console.WriteLine("");


 Console.WriteLine("--------------------------------------------------------------------------------------------");


 Console.WriteLine("Total New Files Copied:" + copy_count);


 Console.WriteLine("Total Newer Files Updated:" + newer_count);


 Console.WriteLine("Total Files Skipped:" + skip_count);


 Console.WriteLine("--------------------------------------------------------------------------------------------");


 Console.WriteLine("");


 Console.WriteLine("");


 }


 }


}




时间:


static public void ArchiveCopyFolder(string sourceFolder, string destFolder)


{


 try


 {


 if (!Directory.Exists(sourceFolder))


 {


 Directory.CreateDirectory(archivefolder + destFolder);


 //Copy folder and files


 }


 }


}



...