Copying files, skipping alert window 'are you sure message'

183 Views Asked by At

My C# console program copies 2 folders into 1. It works but I am new to C#. I cant figure out how I make the program skip the alert window "are you sure you want to overwrite the files".

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using System.Diagnostics;

namespace MirrorSystem {
class Program {
        static void Main(string[] args)
        {
        string source1 = @"folder1";
        string source2 = @"folder2";
        string destination = @"destination";
        try
        {
            Console.WriteLine("Starting..");
            FileSystem.CopyDirectory(source1, destination, UIOption.AllDialogs);
            FileSystem.CopyDirectory(source2, destination, UIOption.AllDialogs);

            Console.WriteLine("Success!");
            System.Threading.Thread.Sleep(5000);
            Environment.Exit(0);
            Console.ReadKey();
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("Canceled!");
            Console.ReadKey();
        }
    }
}
}
2

There are 2 best solutions below

0
On

You can use method File.Copy --> https://msdn.microsoft.com/es-es/library/9706cfs5(v=vs.110).aspx

The third parameter allows you to specify whether you should or should not overwrite existing files with the same name in the destination folder.

4
On

You can pass the value true as the third (overwrite) parameter. Here is the official documentation for the method:

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

The description of the overwrite parameter:

overwrite

Type: System.Boolean

True to overwrite existing files; otherwise False. Default is False.

Source.