Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 874
JimDel Avatar asked Nov 06 '22 07:11

JimDel


1 Answers

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); 
   } 
} 
like image 111
Mitch Wheat Avatar answered Nov 09 '22 12:11

Mitch Wheat