Pages

Monday, August 19, 2013

Create, Read, Write, Copy, Move and Delete File in Asp.net

This article will explaining how to Create, Read, Write, Copy, Move and Delete file in asp.net. Asp.net provides 'System.IO' namespace that enable you to done all type of file operation on your local drive using programming.

Create a Text File.
In this step we will create a text file.

protected void CreateFile_Click(object sender, EventArgs e)
    {
        string location = @"D:\MyTextFile.txt";

        FileStream fileStream = null;
        if (!File.Exists(location))
        {
            using (fileStream = File.Create(location))
            {
                Response.Write("File sucessfully created");
            }
        }
        else
        {
            Response.Write("File allready exists");
        }
    }

Write to Text File.
In this step we will write some text on created text file.

protected void WriteFile_Click(object sender, EventArgs e)
    {
        string location = @"D:\MyTextFile.txt";

        if (File.Exists(location))
        {
            using (StreamWriter streamWriter = new StreamWriter(location))
            {
                streamWriter.Write("Hello Friends................");
                Response.Write("Sucessfull");
            }
        }
        else
        {
            Response.Write("File not exists");
        }
    }

Read from Text File.
In this step we will read content from text file.

  protected void ReadFile_Click(object sender, EventArgs e)
    {
        string location = @"D:\MyTextFile.txt";

        if (File.Exists(location))
        {
            using (TextReader txtRead = new StreamReader(location))
            {
                Response.Write(txtRead.ReadLine());
            }
        }
        else
        {
            Response.Write("File not exists");
        }
    }

Copy Text File.
In this step we will create copy of text file.

  protected void CopyFile_Click(object sender, EventArgs e)
    {
        string location = @"D:\MyTextFile.txt";
        string copylocation = @"D:\MyTextFile2.txt";

        if (File.Exists(location))
        {
            if (File.Exists(copylocation))
                File.Delete(copylocation);
            File.Copy(location, copylocation);
            Response.Write("File copyed to destination");
        }
        else
        {
            Response.Write("File not exists");
        }
    }

Move Text File.
In this step we will move created file to another location.

  protected void MoveFile_Click(object sender, EventArgs e)
    {
        string location = @"D:\MyTextFile.txt";
        string moveLocation = @"E:\MyTextFile" + System.DateTime.Now.Ticks + ".txt";

        if (File.Exists(location))
        {
            File.Move(location, moveLocation);
            Response.Write("File moved to destination");
        }
        else
        {
            Response.Write("File not exists");
        }
    }

Delete to Text File.
In this step we will deleted to created file.

  protected void DeleteFile_Click(object sender, EventArgs e)
    {
        string location = @"D:\MyTextFile.txt";

        if (File.Exists(location))
        {
            File.Delete(location);
            Response.Write("File sucessfully deleted");
        }
        else
        {
            Response.Write("File not exists");
        }
    }

No comments:

Post a Comment