How to manipulate text file using asp.net
In this article, we will see how to create,read,write,copy and move text file using asp.net.
In this article, we will see how to create,read,write,copy and move text file using asp.net.The Namespace that is required to deal with files
We require the namespace, System.IO to work with files. so, we should import this namespace in our asp.net code behind file.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
string strfileLococation = @"c:\Textfile.txt";
protected void btnCreate_Click(object sender, EventArgs e)
{
FileStream fs = null;
if (!File.Exists(strfileLococation))
{
using (fs = File.Create(strfileLococation))
{
Response.Write("File Created Successfully");
}
}
else
{
Response.Write("File already Created");
}
}
protected void btnWrite_Click(object sender, EventArgs e)
{
if (File.Exists(strfileLococation))
{
using (StreamWriter sw = new StreamWriter(strfileLococation))
{
sw.Write("WWW.Dotnetspider.com");
Response.Write("Writed Successfully");
}
}
}
protected void btnRead_Click(object sender, EventArgs e)
{
if (File.Exists(strfileLococation))
{
using (TextReader tr = new StreamReader(strfileLococation))
{
Response.Write("From text file");
Response.Write("
");
Response.Write(tr.ReadLine());
}
}
}
protected void btnCopy_Click(object sender, EventArgs e)
{
string fileLocCopy = @"F:\Textfile.txt";
if (File.Exists(strfileLococation))
{
// If file already exists in destination, delete it.
if (File.Exists(fileLocCopy))
{
File.Delete(fileLocCopy);
}
File.Copy(strfileLococation, fileLocCopy);
Response.Write("File Copied Successfully to " + fileLocCopy);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (File.Exists(strfileLococation))
{
File.Delete(strfileLococation);
}
}
protected void btnMove_Click(object sender, EventArgs e)
{
// To Create unique file name
string strfileLocMove = @"d:\Textfile" + System.DateTime.Now.Ticks + ".txt";
if (File.Exists(strfileLococation))
{
File.Move(strfileLococation, strfileLocMove);
Response.Write("File Moved to " + strfileLocMove);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnCreate" runat="server" OnClick="btnCreate_Click" Text="Create Text File" />
<asp:Button ID="btnWrite" runat="server" OnClick="btnWrite_Click" Text="Write to a Text file" />
<asp:Button ID="btnCopy" runat="server" OnClick="btnCopy_Click" Text="Copy a Text file" />
<asp:Button ID="btnRead" runat="server" OnClick="btnRead_Click" Text="Read a text file" />
<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete Text file" />
<asp:Button ID="btnMove" runat="server" OnClick="btnMove_Click" Text="Move a Text file" /></div>
</form>
</body>
</html>