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 move file from one location to another location and check file exists or not using File class in c#.net 2.0

Author: Zunnair
Download Source Code : 840_io.zip

In this simple article you will learn how to move a file from one location to another location and check while file exists or not

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

Moving a File:

First of all create a source file. Move function takes two parameters source file path and destination path where file should be moved. It also provides us a facility to change the name of file at new location.

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, destination path in second textbox and press button.

Now write the following code on button click event:

C#

private void btn_Move_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)

                {

                      File.Move(str_Source, str_Destination);

                        MessageBox.Show("File Moved");

                }

          

                else

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

            }

        }

VB

Private Sub btn_Move_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

                File.Move(str_Source, str_Destination)

                MessageBox.Show("File Moved")

            Else

 

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

            End If

        End If

    End Sub

This is a simple code to move the file from one location to another.

Check File Exists Or Not:

You can check that file exist on a given path or not.

It takes file path as a parameter and returns bool value. If file exists it returns true else return false.

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 textbox and press button.

Now write the following code on button click event:

C#

private void btn_Exist_Click(object sender, EventArgs e)

        {

            str_exist = txtExist.Text;

            if (str_exist != null)

            {

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

                {                   

                    MessageBox.Show("FILE EXIST");

                }

                else

                {

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

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }  

VB

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

        str_exist = txtExist.Text

        If str_exist IsNot Nothing Then

            If File.Exists(str_exist) = True Then

                MessageBox.Show("FILE EXIST")

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        End If

    End Sub

This simple code will check the existence of a file.

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 move a file from one location to another location and check while file exists or not

Article Comments
This is a very good post sir ...It is very helpfull

Thank you very much.....

Posted on 3/15/2010 3:15:53 AM by pavan

Nice.. But Basic Stuff Only..

Posted on 3/30/2010 4:56:47 AM by Casmir Dominic

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to ENCRYPT and DECRYPT file using SYSTEM.IO namespace in c# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net