Friday, 8 July 2016

Dynamically Create Text File (.txt) Date Wise in asp.net Using C#.Net and VB.Net

7/08/2016 - By Pranav Singh 0

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.

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
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.

 

Here is the file 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

0 comments:

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

GMAIL ID: dotnetpools@gmail.com

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 contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top