ASP.NET – Create and Write Text to File

  • by
ASP.NET Create Write Text to File

ASP.NET – How to Create and Write Text content to a File. Creating and storing data on to a file in server is an important method of storing UserLogs, ErrorLogs and information generated by our website. By storing text contents on to a file we can track our user activities and any exceptions which can be occurred because of errors. In this post we will see How we can create Text file and write text contents to the newly created file in ASP.NET. The simple approach is to use StreamWriter to write to file and File.Create to create the file.

DOWNLOAD SOURCE CODE

Using this method you will be able to create a Text file and write the contents on it:

public void WriteToFile(string fileName, string fileContent)
{
try
{
fileName += ".txt";
string FolderPath = @"D:\FileWriter\";
if (!Directory.Exists(FolderPath))
Directory.CreateDirectory(FolderPath);

if (!File.Exists(FolderPath + fileName))
File.Create(FolderPath + fileName).Close();

StreamWriter stream = new StreamWriter(FolderPath + fileName, true);
stream.WriteLine(fileContent);
stream.Close();

lblInfo.Text = "Text written to file.";
}
catch (Exception ex)
{
lblInfo.Text = "Error occurred:"+ex.Message.ToString();
}
}

This method accepts two String parameters. One for file name and other for the file contents. We are concatenating “.txt” extension. Our folder path is "D:\FileWriter\", here the file will be created. Also the Directory and File will be created if it doesn’t exist already. If the file exists, it will append the Text content in it. stream.WriteLine(fileContent) will write the text on a new line in our Text file.

Now we will create a complete design so that we can create new files.
Edit the Default.aspx file as below:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LoginApp.Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Write Text File</title>
<style>
body {
padding: 20px !important;
margin: 10px !important;
font-family:Verdana;
font-size:16px;
}
.txt
{
width:500px;
padding:5px;
border-radius:5px;
font-size:16px !important;
}
.Contents
{
height:60px;
}
.btn
{
background-color:#fc1303;
color:#fff;
border:0px !important;
border-radius:5px;
padding:5px 15px;
}
.btn:focus,.btn:hover
{
background-color:#000;
color:#fff;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div >
<span>File Name:</span>
<br />
<asp:TextBox runat="server" ID="txtFileName" class="txt" />
<br />
<br />
<span>File Contents:</span>
<br />
<br />
<asp:TextBox TextMode="MultiLine" runat="server" ID="txtFileContent"
class="txt Contents" />
<br />
<br />
<asp:Button runat="server" Text="Submit" ID="btnSubmit" OnClick="btnSubmit_Click"
CssClass="btn" />
<br />
<br />
<asp:Label runat="server" Text="" ID="lblInfo" />
</div>
</form>
</body>
</html>

This design contains to ASP.NET textboxes, one to enter the file name and other to enter the text which we will write on to the file. A ASP.NET button to submit our data and a Label to show the result.

ASP.NET Create Write Text to File

ASP.NET Create Write Text to File

Edit the Code file as below
Default.aspx.cs:

using System;
using System.IO;

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

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (String.IsNullOrWhiteSpace(txtFileName.Text.ToString()) || String.IsNullOrWhiteSpace(txtFileContent.Text.ToString()))
lblInfo.Text = "Please enter File name and its content";
else
WriteToFile(txtFileName.Text.ToString(), txtFileContent.Text.ToString());
}
catch(Exception ex)
{
lblInfo.Text = ex.Message.ToString();
}
}

public void WriteToFile(string fileName, string fileContent)
{
try
{
fileName += ".txt";
string FolderPath = @"D:\FileWriter\";
if (!Directory.Exists(FolderPath))
Directory.CreateDirectory(FolderPath);

if (!File.Exists(FolderPath + fileName))
File.Create(FolderPath + fileName).Close();

StreamWriter stream = new StreamWriter(FolderPath + fileName, true);
stream.WriteLine(fileContent);
stream.Close();

lblInfo.Text = "Text written to file.";
}
catch (Exception ex)
{
lblInfo.Text = "Error occurred:"+ex.Message.ToString();
}
}
}
}

We are calling the WriteToFile method on our button click event and passing the txtFileName.Text as our File name and txtFileContent.Text as our File content to write on to the File. And also checking if the user as entered the name of the file and its content to write, if not we are showing Message on our ASP.NET label control. On pressing submit button we will successfully create a the Text file and write text in it. You can Download Source code for your reference for free.

DOWNLOAD SOURCE CODE


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.

1 thought on “ASP.NET – Create and Write Text to File”

  1. Pingback: ASP.NET C# - Create PDF Invoice using iText7 Library - ParallelCodes

Comments are closed.