Is there a way to get this C# code to automatically overwrite files? – Education Career Blog

Is there a way to get this code to automatically overwrite files?

// Requires project reference to Microsoft.VisualBasic
using Microsoft.VisualBasic.FileIO;
class FileProgress
{
   static void Main()
   {
      string sourcePath = @"C:\Users\public\documents\";
      string destinationPath = @"C:\testFolder";
      FileSystem.CopyDirectory(sourcePath, destinationPath,
          UIOption.AllDialogs);
   }
}

This is from MSDN

Thanks

,

Yes.

use this overload of FileSystem.CopyDirectory to overwrite existing files:

public static void CopyDirectory(
    string sourceDirectoryName,
    string destinationDirectoryName,
    bool overwrite
)

So your code becomes:

// Requires project reference to Microsoft.VisualBasic 
using Microsoft.VisualBasic.FileIO; 
class FileProgress 
{ 
   static void Main() 
   { 
      string sourcePath = @"C:\Users\public\documents\"; 
      string destinationPath = @"C:\testFolder"; 
      FileSystem.CopyDirectory(sourcePath, destinationPath, True); 
   } 
} 

Leave a Comment