Whenever we copy file from one directory to another
directory on that case Window show the progress and file detail, time
remaining, no of file remains to copy and many more which were copy by user. So this article will show you Show Progressbar
While Moving Folder File From One Directory To Other Using C#.Net In Windows
Application.
Some of my previous articles are as follows: How
to Create Column Chart in Windows Application Using C#.Net, Windows
Application - Excel Sheet Name in C#.Net, Bind
And Display Image in a DatagridView Using C#.Net in Windows Application, WaterMark
TextBox In Windows Applications Using C# and VB.Net, Pass
Value From One Form to Another Form in Windows Application C#.Net,VB.Net, How
to Minimize an Application to the Taskbar Tray in C#.Net | System Tray
Application C#, How
to get the selected date of a MonthCalendar control in C#.
For this article first we will create a new windows
application in this window we will add button control and label control. After
adding all the controls your form will look as shown below.
Now generate the button click event. Add the code to copy
file from one directory to another directory.
But before that we need to add library reference for copying the file.
For that first we will right click on References folder.
Now a dialog box will
open in this on .net tab select “Microsoft.VisualBasic”, select this dll
and click on ok button.
After adding this reference
add the below code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void button1_Click(object
sender, EventArgs e)
{
try
{
//Source
from where we copy the file
string
sourceFolderPath = @"D:\backup\Data\Sorce";
//Just for
display purpose
label3.Text =
sourceFolderPath;
//
Destination for copied files.
string
destinationFolderPath = @"D:\backup\Data\Destignation";
//Just
for display purpose
label4.Text =
destinationFolderPath;
FileSystem.CopyDirectory(sourceFolderPath,
destinationFolderPath, UIOption.AllDialogs);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
|
In above code check the Microsoft.VisualBasic.FileIO
is used for FileSystem.CopyDirectory.
Now we have done run the application to check the output.
Now click on "Move File", you will get the dialogbox as shown below.
Now if you click on cancel button you will get the cancel message, Basically it's an exception which i an showing in message box.
On other hand if file moves successfully dialog box will disappear.
Thanks you great post. BTW
ReplyDelete