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
 

Delete and Copy Files from a Folder with respect to their Extension in .NET 2.0 using C#.

Author: DevAsp Team
Download Source Code : 696_folderinfochecker.zip

In this simple article you will learn how delete and Copy Files from a Folder with respect to their Extension.

Simply just create New Project of C#.

 

Drag two button, two labels and two textboxes on the form.

 

Now write code on Form load event

 

  private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp File Checker";

            label1.Text = "Folder Path";

            label2.Text = "File Extension";

        }

 

Write two functoins

public void folderwatcher(string folderpath, string fileExtension)

        {

            System.IO.DirectoryInfo dr = new System.IO.DirectoryInfo(folderpath);

            System.IO.FileInfo[] finfo = dr.GetFiles();

            foreach (System.IO.FileInfo fi in finfo)

            {

                if (fi.Extension.ToUpper() == fileExtension.ToUpper())

                    fi.Delete();

            }

 

        }

        public void fileCopier(string folderpath, string fileExtension)

        {

            try

            {

                System.IO.DirectoryInfo dr = new System.IO.DirectoryInfo(folderpath);

                System.IO.FileInfo[] finfo = dr.GetFiles();

                foreach (System.IO.FileInfo fi in finfo)

                {

                    if (fi.Extension.ToUpper() == fileExtension.ToUpper())

                        fi.CopyTo(folderpath + "\\Copy of " + fi.Name);

                }

            }

            catch (Exception ex)

            {

              

            }

 

        }

 

Now write code on Button Click events

 

private void button1_Click(object sender, EventArgs e)

        {

            folderwatcher(textBox1.Text, "." + textBox2.Text );

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            fileCopier(textBox1.Text, "." + textBox2.Text);

        }

 

 

Now when you Run Application then just give Path of the Folder and Extension of Files.

Article Comments
Must read article so that we can get an idea of files , directories

Posted on 9/24/2007 5:55:51 AM by bharath

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Focus a child form from MDI form .net 2.0 using c#.

Disclaimer - Privacy
© 2002-2017 DevASP.net