Mega Code Archive

 
Categories / C# / File Stream
 

File Copy Method Copies file to a new file

using System; using System.IO; class Test  {     public static void Main()      {         string sourceDir = @"c:\c";         string backupDir = @"c:\a";              string[] picList = Directory.GetFiles(sourceDir, "*.jpg");         string[] txtList = Directory.GetFiles(sourceDir, "*.txt");         foreach (string f in picList)         {             string fName = f.Substring(sourceDir.Length + 1);             File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);         }         foreach (string f in txtList)         {             string fName = f.Substring(sourceDir.Length + 1);             try             {                 File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));             }             catch (IOException copyError)             {                 Console.WriteLine(copyError.Message);             }         }         foreach (string f in txtList)         {             File.Delete(f);         }         foreach (string f in picList)         {             File.Delete(f);         }    } }