Read a text file in ASP.NET

The System.IO namespace contains the StreamReader class allow reading of files.
You can then read the entire contents of the text file into a string, by using ReadToEnd() method.
Remmber to close the StreamReader using the Close() method when finished.

 <!DOCTYPE html>

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As New System.IO.StreamReader(Server.MapPath("test.txt"))
        Dim sContent As String = sr.ReadToEnd
        sr.Close()
        Response.Write(sContent)
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
Updated: March 6, 2015 — 4:59 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Charon © 2015 Privacy Policy | Terms & Conditions Frontier Theme