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 replace file with another file and DELETE file using system.io namespace in c# (.net 2.0).

Author: Zunnair
Download Source Code : 841_io.zip

In this simple article you will learn how to replace a file with another file with different name and delete an existing file

 File class provide us a static functions Replace() and Delete(). These are static functions so they can be called by class name.

 Replacing a File:

First of all create a source file and destination files with some data. Replace function takes three parameters source file path destination file path and destination file path with backup or new name.

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

Now write the following code on button click event:

C#

private void btn_Replace_Click(object sender, EventArgs e)

        {

            str_Source = txtSource.Text;

            str_Destination = txtDestination.Text;

            str_backup = txtBackup.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.Replace(str_Source, str_Destination, str_backup);

                        MessageBox.Show("Data REPLACED");

                    }

                    else

                    {

                         MessageBox.Show("FILE NOT EXISTED");

                    }

               }

                else

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

            }

        }

VB

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

        str_Source = txtSource.Text

        str_Destination = txtDestination.Text

        str_backup = txtBackup.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.Replace(str_Source, str_Destination, str_backup)

                    MessageBox.Show("Data REPLACED")

                Else

                    MessageBox.Show("FILE NOT EXISTED")

                End If

            Else

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

            End If

        End If

    End Sub

This function does nothing if source or destination file is missing.

Delete File:

You delete a file by using static function Delete() of File class.

It takes file path as a parameter and deletes it. If file does not exist it does not throw an exception.

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

Now write the following code on button click event:

C#

private void btn_delete_Click(object sender, EventArgs e)

        {

            str_delete = txtDelete.Text;

            if(str_delete!=null)

            {

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

                {

                File.Delete(str_delete);

                MessageBox.Show("FILE DELETED");

                }

                else

                {

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

                }

            }

            else

            {

            MessageBox.Show("please write path");

            }

       }  

VB

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

        str_delete = txtDelete.Text

        If str_delete IsNot Nothing Then

            If File.Exists(str_delete) = True Then

                File.Delete(str_delete)

                MessageBox.Show("FILE DELETED")

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        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 how to replace a file with another file with different name and delete an existing file

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to move file from one location to another location and check file exists or not using File class in c#.net 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net