Create a text file in ASP.NET

We need the namespace, System.IO to use files.

The method, CreateText requires a string for an argument. The string is the path of file which will be created. After we have correctly written to the text file, we close the StreamWriter by invoking the Close method of StreamWriter. See below for example code.

<!DOCTYPE html>

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim TextFile As New System.IO.StreamWriter(Server.MapPath("test.txt"))
        TextFile.WriteLine("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
    End Sub


<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" />
Updated: June 30, 2020 — 9:16 pm