This article will show you how you can dynamically
create text file (.txt) date wise in asp.net using c#.net and
vb.net. In this First we will create text file in a directory and then write
some text on it.
Some
of my previous articles are as follows: Business
Email Validation Using RegularExpressionValidator Control in Asp.net, Convert
DataTable To JSON Object/Data In ASp.Net Using C#.Net, Display
Latitude and Longitude Value on Google Map from Sql Server Data Base in Asp.net
Using C#.Net, Show
Loading While Page Loads Using jQuery in Asp.net, Credit
Card Data Format Validation Using jQuery In Asp.Net, Enable
and Disable Asp.Net FileUpload Control Using JavaScript, Enable
and Disable Asp.Net FileUpload Control Using jQuery, How
to Enable or Disable Requiredfieldvalidator in Asp.Net Using jQuery, Dynamically
Display Images From Folder By Ajax Using jQuery In Asp.net ,C#, Read
and Show CSV File Data In DataList Using C#.Net in Asp.net.
So for this article first we will create a new
asp.net application and create directory for saving the created text files.
Now we will add a new asp.net page add some
controls in it.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm9.aspx.cs" Inherits="WebApplication7.WebForm9" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamically Create
Text File (.txt) Date Wise in asp.net Using C#.Net and VB.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
File
Name:
<asp:TextBox ID="txtFileName"
runat="server" Width="372px"></asp:TextBox><br />
Enter
Text:
<asp:TextBox ID="txtFileContent" runat="server" Width="372px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Generate Text File" OnClick="Button1_Click"
/><br />
<asp:Label ID="lblmessage"
runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
|
Now we will generate the button click event
and add the code on the page.
C#.Net
C#.Net
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 WebForm9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void
CreationOfTextFile(string filename, string message)
{
if (!Directory.Exists(Server.MapPath("\\TextFile")))
{
Directory.CreateDirectory(Server.MapPath("\\TextFile"));
}
string txtFilename =
filename + ".txt";
string txtFilePath = @"\TextFile\"
+ txtFilename;
//Validate log file exist or not
if (!File.Exists(Server.MapPath(txtFilePath)))
{
File.Create(Server.MapPath(txtFilePath));
}
message = DateTime.Now.ToString() + "
: " + message;
StreamWriter file
= new StreamWriter(Server.MapPath(txtFilePath),
true);
file.Write(message + "\r\n");
file.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string message =
txtFileContent.Text;
string filename =
txtFileName.Text;
CreationOfTextFile(filename, message);
lblmessage.Text = "File
Created successfully";
}
}
}
|
VB.Net
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 WebForm9
Inherits
System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Public Sub
CreationOfTextFile(filename As String, message As String)
If Not
Directory.Exists(Server.MapPath("\TextFile")) Then
Directory.CreateDirectory(Server.MapPath("\TextFile"))
End If
Dim txtFilename As String = filename &
Convert.ToString(".txt")
Dim txtFilePath As String =
Convert.ToString("\TextFile\") & txtFilename
'Validate log file exist or not
If Not
File.Exists(Server.MapPath(txtFilePath)) Then
File.Create(Server.MapPath(txtFilePath))
End If
message = Convert.ToString(DateTime.Now.ToString() + " : ")
& message
Dim file__1 As New StreamWriter(Server.MapPath(txtFilePath), True)
file__1.Write(message & Convert.ToString(vbCr & vbLf))
file__1.Close()
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim message As String =
txtFileContent.Text
Dim filename As String =
txtFileName.Text
CreationOfTextFile(filename, message)
lblmessage.Text = "File
Created successfully"
End Sub
End Class
End Namespace
|
In above code I have created a method “CreationOfTextFile”
which will create the text file and write the text on it.
So in above code first I have checked weather
the directory exists or not. If it exists at that time we will proceed further
otherwise create the folder.
Now we will check for the provided file name
if the file exist then we will not create otherwise we will create the file.
Now we will write the code to the text file. Now
we have done. Run the application to check the output.
Now check the directory.
0 comments:
Please let me know your view