Results 1 to 4 of 4

Thread: Read From txt file with VB.NET? What's different?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Read From txt file with VB.NET? What's different?

    VB.NET is not liking my code to open a text file for read access. I'm trying to populate a listbox with the contents of a text file, but .NET does not recognize the following code:

    Code:
    Dim strFile As String
            strFile = "\support files\chronill.dat"
    Open strFile for input as #1
    What is different in VB.NET from 6.0 as far as this goes?

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Read From txt file with VB.NET? What's different?

    See the System.IO namespaces it contains all the classes you need...

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Read From txt file with VB.NET? What's different?

    You need to use a streamreader to read from a file.
    VB Code:
    1. Imports System.IO
    2.  
    3. Private Sub btnReadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFromFile.Click
    4.  
    5.     Dim myStreamReader As StreamReader
    6.  
    7.     myStreamReader = File.OpenText(txtFileName.Text)
    8.     Me.txtFileText.Text = myStreamReader.ReadToEnd()
    9.  
    10. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Read From txt file with VB.NET? What's different?

    In .NET there is a totaly new way to read from file, StreamReader

    VB Code:
    1. Dim AReadedLine As String ' The buffer string that will contain the strings read
    2. Dim MyStreamReader As System.IO.StreamReader = New System.IO.StreamReader("MyFileName") ' Open the file
    3. AReadedLine = MyStreamReader.ReadLine() ' Read a line from the file
    Using VS 2010 on Fw4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width