C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Write into a Text File Line-By-Line


Posted Date: 20 Jun 2008    Resource Type: Code Snippets    Category: File Operations
Author: BharathiMember Level: Diamond    
Rating: Points: 5



The sample code is a simple ASP.NET application that writes the contents of a TextBox into a .txt file. The code behind page is simple - it contains a form with a text box and a button. On click of a Button, the TextBox value has to be written line by line to a textfile.

The code behind file tries to create the text file if it does not exist. And then appends the contents of the TextBox into the file.

The code and design are shown below:

Design:


<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="Write to a Text File" Width="153px"/>
</div>
</form>
</body>


Code behind file (.cs):


protected void btn_Click(object sender, EventArgs e)
{
StreamWriter s = null;
if (File.Exists("h://TEST.txt"))
{
s = new StreamWriter("h://TEST.txt", true);
}
s.WriteLine(txt.Text);
s.Close();
}


Attachments

  • Design Page (19013-2054-WriteaFile-TextFile.aspx.txt)
  • Code Behind File (19013-2055-WriteaFile-TextFile.aspx.cs.txt)



  • Responses

    Author: RamyaNaidu    06 Aug 2008Member Level: Silver   Points : 2

    Dim writer As StreamWriter = File.CreateText("E:\myfile.txt")
    writer.WriteLine("Out to file.")
    writer.Close()

    Dim str As String = "Text data"
    Dim num As Integer = 123
    Dim binWriter As New BinaryWriter(File.OpenWrite("E:\myfile.dat"))
    binWriter.Write(str)
    binWriter.Write(num)
    binWriter.Close()


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Write into a text file  .  Log contents of a textbox  .  Copy contents of a textbox to a file  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Convert excel file in HTML format
    Previous Resource: Upload and downloading files to and from Server
    Return to Discussion Resource Index
    Post New Resource
    Category: File Operations


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design


    Contact Us    Privacy Policy    Terms Of Use