Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How we can Copy and Delete Folders with C#.net application in .NET 2.0.

Author: DevASP
Download Source Code : 623_CopyFolder.zip

In this simple article you will learn that how you can Copy and Delete Folders.

Simply just create New Project of Windows application in C#. Drag two text box and two buttons on form.This Application Copy And Delete Specific file using .net interface. This is very use full Application for Beginners.Insert FolderBrowserDialog in your application.Now just write code against these events

 

 

private void Button2_Click(object sender, EventArgs e)

        {

            folderBrowserDialog1.ShowDialog();

            inp.Text = folderBrowserDialog1.SelectedPath;

            destination = inp.Text;

 

        }

 

        private void Button1_Click(object sender, EventArgs e)

        {

            folderBrowserDialog1.ShowDialog();

            outt.Text = folderBrowserDialog1.SelectedPath;

            destination = outt.Text;

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            if (inp.Text != "" && outt.Text != "")

            {

                try

                {

 

                    DirectoryInfo diSource = new DirectoryInfo(inp.Text);

                    DirectoryInfo diTarget = new DirectoryInfo(outt.Text);

 

                    foreach (FileInfo fi in diSource.GetFiles())

                    {

                        fi.CopyTo(Path.Combine(diTarget.ToString(), fi.Name), true);

                    }

 

 

 

                    //f.CopyTo(destination + "\\" + f.Name);

                    MessageBox.Show("File " + diSource.Name + " Copied");

 

 

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

            }

            else

                MessageBox.Show("Select Source and Destination");

 

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            if (inp.Text != "")

            {

                DirectoryInfo diSource = new DirectoryInfo(inp.Text);

                foreach (FileInfo fi in diSource.GetFiles())

                {

                    fi.Delete();

                }

                diSource.Delete();

                MessageBox.Show(diSource.Name + " is Deleted");

                inp.Text = "";

            }

            else

                MessageBox.Show("Select Folder in Source");

 

          

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Product";

            this.BackColor = Color.Ivory;

        }

Article Comments
fine illustration.

Posted on 12/8/2006 7:25:50 AM by Iftikhar Qamar

REally Really nice Article!!!!!
Awsome!

Posted on 6/28/2007 2:57:46 AM by sajid

Hi,
This artical realy help me. its realy good one.

Thanx a lot.
Regard Bachan

Posted on 11/7/2007 3:27:31 AM by bachan

I think its right way and it will work fine

Posted on 12/27/2007 7:48:00 AM by Abrar Ahmad

Zip file 623_CopyFolder.zip is identical to
597_CopyFile.zip. There is no CopyFolder sample.
CopyFile sample is great.
Thanks

Posted on 1/21/2008 3:09:46 PM by N.Grant

very nice article good, very good

Posted on 5/7/2008 9:35:52 AM by Rajendra Ram

HI,Thanks to put this in the Web its helpfull to me.
How to delete a single file in the directory?
plz send solution.

Posted on 6/25/2008 1:19:49 AM by Srinivas

Thank you....
This article is really help me.....
Very nice illustration......
once again thank you.........

Posted on 8/11/2008 12:14:23 PM by Amitha M.D

its good but its not complete, what if the folder has sub folders they wont apper in get Files ...

thank you for your work but it didnt help me .

Posted on 8/17/2008 10:07:20 PM by The Night Phoenix

Nice code if you have any code to delete folder and its subfolder and their content in asp.net(c#) then reply
Thanks

Posted on 9/1/2008 9:37:01 AM by MD Zakir Karim Khan

Its great thank you again and again.........

Posted on 11/11/2008 1:07:01 AM by Vindhya

Better way to Delete the Folder recursively is

Directory.Delete("FolderName", true);

Posted on 12/15/2009 6:22:23 AM by Pradeep

Hi,
This program just copies the files recursively one by one from one folder to other. In case the folder are to be copied exist on server it takes lot of time for the files to get copied. Is there any better way to acheive this

Posted on 12/16/2009 7:31:30 AM by Rakesh kumar

Someone always comes up with with Directory.Delete("xxx",true), but IT DOESN'T WORK WHEN THERE ARE FILES.
This works
public void RemoveDirectory(string dir)
{
DirectoryInfo dirInfo = new DirectoryInfo(dir);
RemoveDirFiles(dirInfo);

foreach (DirectoryInfo subDir in dirInfo.GetDirectories("*.*"))
{
RemoveDirectory(subDir.FullName);

}
dirInfo.Delete();
}

public void RemoveDirFiles(DirectoryInfo dirInfo)
{
foreach (FileInfo file in dirInfo.GetFiles("*.*"))
{
file.Delete();
}
}

Posted on 1/13/2010 4:21:00 AM by Adrian

01.private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
02. {
03. bool ret = false;
04. try
05. {
06. SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
07. DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
08.
09. if (Directory.Exists(SourcePath))
10. {
11. if (Directory.Exists(DestinationPath) == false)
12. Directory.CreateDirectory(DestinationPath);
13.
14. foreach (string fls in Directory.GetFiles(SourcePath))
15. {
16. FileInfo flinfo = new FileInfo(fls);
17. flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
18. }
19. foreach (string drs in Directory.GetDirectories(SourcePath))
20. {
21. DirectoryInfo drinfo = new DirectoryInfo(drs);
22. if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
23. ret = false;
24. }
25. }
26. ret = true;
27. }
28. catch (Exception ex)
29. {
30. ret = false;
31. }
32. return ret;
33. }

Posted on 5/4/2010 7:03:32 AM by Sandeep K Midgule

Hi,
Your post is fantastic. But I need to know how can I delete selected files from a folder of a sever.

Posted on 11/9/2010 7:10:35 AM by Mosaddek

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How We Can Make a windows application which shows Running Processes:

Disclaimer - Privacy
© 2002-2017 DevASP.net