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 to Copy file from one location to another with different name (1 overloaded function) using “FILE CLASS” using SYSTEM.IO in .net 2.0.

Author: Zunnair
Download Source Code : 837_io.zip

In this simple article you will learn how to copy a file from one location to another by different name and also with overwriting facility if same file name is already existed using FILE class in C# .

 File class provide us a static (Copy()) function with one overloaded method. These are static functions so they can be called by class name.

 Copy File Without Overwrite:

First of all create a source file and write some data in it. Now make sure that there does not exist any file with same name of destination file at the destination file location other wise program will throw an exception.

To demonstrate make a window application. Drag two text boxes and one button on the form. Write source file path (like c:\\abc.txt) in the first textbox and destination file path in second textbox than press button.

Now write the following code on button click event:

C#

private void btn_copy_Click(object sender, EventArgs e)

        {

            str_Source=txtSource.Text;

            str_Destination = txtDestination.Text;

            if (str_Source == null || str_Destination == null)

            {

                MessageBox.Show("SOUC OR DETNATION FILE PATH MISSING");

            }

            else

            {

                if (File.Exists(str_Source) == true)

                {

                    if (File.Exists(str_Destination)== true)

                    {

                        File.Copy(str_Source, str_Destination);

                        MessageBox.Show("Data COPIED");

                    }

                    else

                    {

                        File.Copy(str_Source, str_Destination);

                        if (File.Exists(str_Destination)==true)

                        {

                            MessageBox.Show("FILE CREATED AND Data COPIED");

                        }

                        else

                            MessageBox.Show("invalid file path for destination ");

                    }

                }

                else

                    MessageBox.Show("SOURCE FILE DOES NOT EXIST");

            }

        }

VB

Private Sub btn_copy_Click(ByVal sender As Object, ByVal e As EventArgs)

        str_Source = txtSource.Text

        str_Destination = txtDestination.Text

        If str_Source Is Nothing OrElse str_Destination Is Nothing Then

            MessageBox.Show("SOUC OR DETNATION FILE PATH MISSING")

        Else

            If File.Exists(str_Source) = True Then

                If File.Exists(str_Destination) = True Then

                    File.Copy(str_Source, str_Destination)

                    MessageBox.Show("Data COPIED")

                Else

                    File.Copy(str_Source, str_Destination)

                    If File.Exists(str_Destination) = True Then

                        MessageBox.Show("FILE CREATED AND Data COPIED")

                    Else

                        MessageBox.Show("invalid file path for destination ")

                    End If

                End If

            Else

                MessageBox.Show("SOURCE FILE DOES NOT EXIST")

            End If

        End If

    End Sub

Copy File Without Overwrite:

First of all create a source file and write some data in it. Now you do not need to make sure that there exists any file with same name of destination file at the destination file location because this overloaded function provides us a facility to overwrite the existing file with same name.

To demonstrate make a window application. Drag two text boxes and one button on the form. Write source file path (like c:\\abc.txt) in the first textbox and destination file path in second textbox than press button.

Now write the following code on button click event:

C#

private void btn_CpyOverwrite_Click(object sender, EventArgs e)

        {

            str_Source = txtSource.Text;

            str_Destination = txtDestination.Text;

            if (str_Source == null || str_Destination == null)

            {

                MessageBox.Show("SOUC OR DETNATION FILE PATH MISSING");

            }

            else

            {

                if (File.Exists(str_Source) == true)

                {

                    if (File.Exists(str_Destination) == true)

                    {

                        File.Copy(str_Source, str_Destination,true);

                        MessageBox.Show("Data COPIED");

                    }

                    else

                    {

                        File.Copy(str_Source, str_Destination,true);

                        if (File.Exists(str_Destination) == true)

                        {

                            MessageBox.Show("FILE CREATED AND Data COPIED");

                        }

                        else

                            MessageBox.Show("invalid file path for destination ");

                    }

                }

                else

                    MessageBox.Show("SOURCE FILE DOES NOT EXIST");

            }

        }        

}

VB

Private Sub btn_CpyOverwrite_Click(ByVal sender As Object, ByVal e As EventArgs)

        str_Source = txtSource.Text

        str_Destination = txtDestination.Text

        If str_Source Is Nothing OrElse str_Destination Is Nothing Then

            MessageBox.Show("SOUC OR DETNATION FILE PATH MISSING")

        Else

            If File.Exists(str_Source) = True Then

                If File.Exists(str_Destination) = True Then

                    File.Copy(str_Source, str_Destination, True)

                    MessageBox.Show("Data COPIED")

                Else

                    File.Copy(str_Source, str_Destination, True)

                    If File.Exists(str_Destination) = True Then

                        MessageBox.Show("FILE CREATED AND Data COPIED")

                    Else

                        MessageBox.Show("invalid file path for destination ")

                    End If

                End If

            Else

                MessageBox.Show("SOURCE FILE DOES NOT EXIST")

            End If

        End If

    End Sub

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

 

            this.Text = "Devasp.net FILE HANDLING Application";

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 

        Me.Text = "Devasp.net FILE HANDLING Application"

 End Sub

 

This simple article tells you how to copy a file from one location to another by different name and also with overwriting facility if same file name is already existed using FILE class

Article Comments
Nice one

Posted on 7/29/2010 5:33:50 AM by Rajani

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Create File and Append text in files using “FILE CLASS” using SYSTEM.IO in c# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net