promotion image of download ymail app
Promoted
Anonymous
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

Relevance
  • 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...

    <script runat="server">

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

    'simplicity

    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

    Next

    'close the StreamReader

    fileStreamReader.Close()

    End Sub

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

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

    <title>Delimiting Text</title>

    </head>

    <body>

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

    </form>

    </body>

    </html>

    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.