Thursday, 2 June 2016

Write New Line Text In Text File(.txt) in Asp.Net Using C#.Net and VB.Net

6/02/2016 - By Pranav Singh 0

This article will show you how you can add text into text file and show the text into new every time when you add in using c#.netand

So for this article first we will create a new application and add the button and text box control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm13.aspx.cs" Inherits="WebApplication7.WebForm13" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title>Write New Line Text In Text File (.txt) in Asp.Net Using C#</title>

    <form id="form1" runat="server">
            Enter Text:
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />
        <asp:Label ID="lblMessage" runat="server" Text="" style="color: #FF0000"></asp:Label>

Now add the below code to add textbox text into the text file(.txt).

C#.Net Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
    public partial class WebForm13 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)


        protected void Button1_Click(object sender, EventArgs e)
                string message = TextBox1.Text;
                lblMessage.Text = "Text added successfully.";
            catch (Exception ex)
                lblMessage.Text = ex.Message.ToString();
        public void WriteToTextFile(string message)
            if (!Directory.Exists(Server.MapPath("\\TextFile")))
            string logFilename = "TextFile_" + DateTime.Now.ToLongDateString() + ".txt";
            string logFilePath = @"\TextFile\" + logFilename;
            //Validate log file exist or not
            if (!File.Exists(Server.MapPath(logFilePath)))
            message = DateTime.Now.ToString() + " : " + message;
            StreamWriter file = new StreamWriter(Server.MapPath(logFilePath), true);
            file.Write(message + "\r\n");


VB.Net Code:
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace WebApplication7
    Partial Public Class WebForm13
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(sender As Object, e As EventArgs)

        End Sub

        Protected Sub Button1_Click(sender As Object, e As EventArgs)
                Dim message As String = TextBox1.Text
                lblMessage.Text = "Text added successfully."
            Catch ex As Exception
                lblMessage.Text = ex.Message.ToString()
            End Try
        End Sub
        Public Sub WriteToTextFile(message As String)
            If Not Directory.Exists(Server.MapPath("\TextFile")) Then
            End If
            Dim logFilename As String = "TextFile_" + DateTime.Now.ToLongDateString() + ".txt"
            Dim logFilePath As String = Convert.ToString("\TextFile\") & logFilename
            'Validate log file exist or not
            If Not File.Exists(Server.MapPath(logFilePath)) Then
            End If
            message = Convert.ToString(DateTime.Now.ToString() + " : ") & message
            Dim file__1 As New StreamWriter(Server.MapPath(logFilePath), True)
            file__1.Write(message & Convert.ToString(vbCr & vbLf))

        End Sub
    End Class
End Namespace

In above code I have first checked the directory and then checked the text file is there or not.  After confirmation of file status I have written the code to add the text into the text file. In this code please check the bold part of the code. In this streamwriter we need to pass true. And while writing text to file must append “\r\n” to the message.

Now we have done. Run the application to check the output.

Now check the text file for output

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post


Please let me know your view

Free Ebooks

About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

Email Id :

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at


Top jQuery Plugins

Global Classified :
© 2014
back to top