promotion image of download ymail app
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

Read delimited text files into text files (Visual Basic .net)?

I need to read the content of a comma delimited text file into several textboxes on the screen. I looked at an MSDN article on the topic but it didn't explain it well enough.

1 Answer

  • 1 decade ago
    Favorite Answer

    Here is the content of an ASPX web page using VB.Net:

    <%@ Page Language="VB" %>

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

    <script runat="server">

    'Note: This should be placed in the web forms code behind file but is placed here for


    Sub page_load(ByVal Sender As Object, ByVal e As EventArgs)

    'dimension the input file variable to type of String

    Dim inputFile As String

    'get the input file path

    inputFile = Server.MapPath("delimitedFile.txt")

    'dimension a variable to type of StreamReader

    Dim fileStreamReader As StreamReader

    'open the text delimited file in the StreamReader

    fileStreamReader = File.OpenText(inputFile)

    'dimension a variable to type of String to hold the file contents

    Dim fileStreamContents As String

    'get the text delimited file content

    fileStreamContents = fileStreamReader.ReadToEnd()

    'dimension a variable to type of String for the text delimiter

    Dim theDelimiter As String

    'the delimiter is a comma

    theDelimiter = ","

    'split the delimited text into a String Array

    Dim splitText = Split(fileStreamContents, theDelimiter)

    'form label to hold the input file content

    lblPlainText.Text = fileStreamContents & "<br>"

    'dimension a variable to type of Integer for the number

    'of each iteration

    Dim i As Integer

    'iterate through the String Array

    For i = 0 To UBound(splitText)

    If i = 0 Then 'remember that a String Array is zero based

    TextBox1.Text = splitText(i)

    ElseIf i = 1 Then

    TextBox2.Text = splitText(i)

    ElseIf i = 2 Then

    TextBox3.Text = splitText(i)

    End If


    'close the StreamReader


    End Sub


    <html xmlns="" >

    <head id="Head1" runat="server">

    <title>Delimiting Text</title>



    <form id="form1" runat="server">

    <asp:label align="center" runat="server" id="lbldisplay" Font-Names="Verdana" text="Reading a delimited text file using ASP.NET/VB.NET" /><br />

    <br /><b>Your File Input</b><br />

    <asp:label runat="server" id="lblPlainText" Font-Names="Verdana" /><br />

    <b></b><strong>Text Boxes</strong><br />

    Text Box one<br />

    <asp:textbox id="TextBox1" runat="server"></asp:textbox><br />

    <br />

    Text Box two<br />

    <asp:textbox id="TextBox2" runat="server"></asp:textbox><br />

    <br />

    Text Box three<br />

    <asp:textbox id="TextBox3" runat="server"></asp:textbox><br />




    Here is the content of the delimitedFile.txt file that is parsed above:

    box 1 content, box 2 content, box 3 content

    You will need to have the 1.1 .Net framework on your system along with IIS for this to work. If it is a Windows Form (Desktop app) then you can still use the above Sub.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.