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 asp.net using c#.netand VB.net.
Some of my previous articles are as follows: How
to Read a Text File Line by Line in C# in Console Application, How
to Read Text File Each Line Using c# In Asp.net, How
to Extract Text From PDF File Using C#.Net.
So for this article first we will create a new asp.net 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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Write New Line
Text In Text File (.txt) in Asp.Net Using C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter
Text:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />
</div>
<asp:Label ID="lblMessage"
runat="server" Text="" style="color:
#FF0000"></asp:Label>
</form>
</body>
</html>
|
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)
{
try
{
string message =
TextBox1.Text;
WriteToTextFile(message);
lblMessage.Text = "Text added
successfully.";
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
}
public void WriteToTextFile(string message)
{
if (!Directory.Exists(Server.MapPath("\\TextFile")))
{
Directory.CreateDirectory(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)))
{
File.Create(Server.MapPath(logFilePath));
}
message = DateTime.Now.ToString() + "
: " + message;
StreamWriter file = new StreamWriter(Server.MapPath(logFilePath),
true);
file.Write(message + "\r\n");
file.Close();
}
}
}
|
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)
Try
Dim message As String = TextBox1.Text
WriteToTextFile(message)
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
Directory.CreateDirectory(Server.MapPath("\TextFile"))
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
File.Create(Server.MapPath(logFilePath))
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))
file__1.Close()
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.
0 comments:
Please let me know your view