Creating Text Files in ASP.NET using VB.NET

In this article you will learn about how to create Text Files with StreamWriter object and the mechanism to capture the errors which comes while creating a text file.
  • 3535
Download Files:
 

Introduction
 
One of the Frequent task in any type of web application is dealing with text files. In this article I am explaining how to create a text file in ASP.NET. The implementation of this needs the namespace that is required to dael with files, the StreamWriter object. We will also use a mechanism in our code to capture the error which may occur while creating a text file.
 
Namespace

We require the namespace, System.Io to work with files. So we should import this namespace in our ASPX page such as.

<%@ Import Namespace="System.IO" %>

How to create a text file


If you want to create a text file you need to create an instance of the object, StreamWriter. The instance will be File Pointer for you. Once you have a file Pointer. You need to invoke the method, CreateText method of the object, File. the method CreateText takes a string as an argument. The string is nothing but the path of the file that is going to get Created. Now see the example, lets assume you have a TextBox and a Button. The TextMode property of the TextBox set to multiline. OnClick event of the button you need to Create a text file.
 
Getting Started

  • Simply create a new ASP.NET web application. 
  • Drag one TextBox, Button and a Label control on page. The page will look like below given page.


     
    TextFile1.gif
  • Then write the below Code.
     
    <%@ Page Language="vb" %>
    <%@ Import Namespace="System.IO" %>
    <html>
      <head>
      <title>Writing to a Text File</title>
    <script runat="server">
        Sub WriteToFile(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim fp As StreamWriter
            Try
                fp = File.CreateText(Server.MapPath(".\Upload\") & "test.txt")
                fp.WriteLine(txtMyFile.Text)
                lblStatus.Text = "File Succesfully created!"
                fp.Close()
            Catch err As Exception
                lblStatus.Text = "File Creation failed. Reason is as follows <br><br>" & err.ToString()
            Finally
            End Try
        End Sub
    </script>
    </
    head>
    <
    body style="font: 10pt verdana">
                <h3 align="center">Creating a Text File in ASP .NET</h3>
        <form id="Form1" method="post" runat="server">
                            Type your text:
                            <asp:TextBox
                                        ID="txtMyFile"
                                        TextMode="MultiLine"
                                        Rows="10"
                                        Columns="60"
                                        Runat="server" /><br/>
                            <asp:button
                                        ID="btnSubmit"
                                        Text="Create File"
                                        OnClick="WriteToFile"
                                        Runat="server" />
                            <asp:Label
                                        ID="lblStatus"
                                        Font-Bold="True"
                                        ForeColor="#ff0000"
                                        Runat="server" />
    </
    form>
    </body>
    </
    html>
     
  • Now run your application.

     

Output:-
TextFile2.gif


Summary

In this article you learned how to create a Text File on Button Click event in ASP.NET using VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.