How To Display a Progress Dialog Box For Copying File Using C#

Introduction

In this article we will learn how to display a copy file dialog box when copying a file from a source to a destination in our project using C#.

Copying a file from Source to Destination

Procedures

Step 1: Create a new “Console Application” (C#) in Visual Studio and name it as you choose (I named it "ProgressDialogBoxDemo").

Now a new Program.cs is generated.

Step 2: Now go to the Solution Explorer and right-click on the "References" item and select "Add Reference".

Add Reference

Step 3: A reference Dialog Box is shown, now navigate to the .NET tab and search for "Microsoft.VsiualBasic" and add it.

NET tab

Step 4: Go to the "Program.cs" file in your project and add the following using directive in your project:

using Microsoft.VisualBasic.FileIO;

Step 4: Add the following code into the Main method in Program.cs:
 

static void Main(string[] args)

{

    //Setting up my Source File Path

    string source = @"F:\Project\CSharp";

    //Setting up my Destination Path

    string destination = @"E:\Destin Pro";

    try

    {

        Console.WriteLine("Start Copying....");

        //Copying the File From Source to

        // Destination

        FileSystem.CopyDirectory(source,

        destination, UIOption.AllDialogs);

        Console.WriteLine("SuccessFully Copied!!!");

        Console.ReadKey();

    }

     //If the Copying Operation is Cancelled in

    // the midway

    //then it throws an Exception which is

     //carried by this catch block

    catch (OperationCanceledException)

    {

        Console.WriteLine("Copying Cancelled!!!");

        Console.ReadKey();

    }

} 

Note: Make sure to change the Source and Destination file path names as it depends upon your System file paths. Also try to copy some large file so that the Progress Dialog Box will be visible to you otherwise you won't able to see the Dialog Box.

Step 5: Now compile it and run it. You will see that your file is being copied from the Source to Destination File Path and a progress dialog box is shown.

Output

Output 

That's all for this article. I am including the source file so that you can go through it.


Similar Articles