dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersPhagu Mahato
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

How to manipulate text file using asp.net


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 20


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>






Did you like this resource? Share it with your friends and show your love!


Responses to "How to manipulate text file using asp.net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to filter only numeric values in the SQL SERVER table?
    Previous Resource: What is ASP.NET Caching? How to use Caching in ASP.NET ?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    How to manipulate text file using asp.net  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.