CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2004
    Posts
    158

    How do I make this work in vb.net?

    I am trying to read/write a simple text file in net 2008 but can't, as yet, get it to work.
    Any help would be appreciated.
    This is how I use to do it in VB6



    Code:
    MyStr = App.Path & "\Data\Data.txt"
    
    Open MtyStr For Input As #1
    Do While Not EOF(1)
    Input #1, Num(a, 0), Num(a, 1), Num(a, 2), Num(a, 3), Num(a, 4), Num(a, 5), Num(a, 6)
    
     a = a + 1
     
    Loop
    Close #1
    Last edited by dajunka; May 9th, 2010 at 12:45 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How do I make this work in vb.net?

    Look up the StreamReader() and StreamWriter() classes. That's the way to access files in VB.Net

    Try these samples, also: http://code.msdn.microsoft.com/vbsamples/
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2004
    Posts
    158

    Re: How do I make this work in vb.net?

    I found info about the streamreader/writer in my book and got it to work, the problem is that it reads/ writes the whole thing, I was looking for a way to read data individually separated by commas. Would you know of a simple way to do this?

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How do I make this work in vb.net?

    The best way or at least a way I often use is to read the entire file and then use the split method to place it into an array. This works very well and is very fast.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Apr 2004
    Posts
    158

    Re: How do I make this work in vb.net?

    And there's me thinking it was going to be so easy.

    Just need to find something helpful on splitting now.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How do I make this work in vb.net?

    It is actually very easy.

    Use the streamreader to read either a line or the entire file into a string variable.

    Dim an array and use the split function on your previous variable to break it down into each element.

    Code:
    Dim MyFile as new System.IO.StreamReader("\MyFile.Txt")
    Dim FileContent as string=MyFile.ReadToEnd()
    MyFile.Close
    Dim FileFields as string()=FileContent.Split(",")
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Apr 2004
    Posts
    158

    Re: How do I make this work in vb.net?

    Cool, thank you.
    This is my code so far and it seems to be working very well except for the splitting bit.

    Code:
     a = 0
            Dim objfile As New System.IO.StreamReader("c:\data.txt")
             Nationaldate = objfile.ReadLine()
                    Do Until Nationaldate Is Nothing
                                 
     ListNational.Items.Add(Nationaldate)
     Nationaldate = objfile.ReadLine()
                a = a + 1
            Loop
            objfile.Close()
            objfile.Dispose()
    Label1.text= "Line Count = " & a+1
    It loads in over 1000 lines, each line has 7 data entries.
    How would I insert the split command to read the 7 separate data entries into a array at each loop?

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How do I make this work in vb.net?

    Split Nationaldate from inside the loop, and load the array (based on the value of a)

    (a,0),(a,1),(a,3)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Apr 2004
    Posts
    158

    Re: How do I make this work in vb.net?

    The examples I have are a little unclear as to the wording or layout I would use to split it into a array (a, 0 ) through to (a,6) and my first few attempts have failed.

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I make this work in vb.net?

    VB.NET String Functions

    I hope it helps a bit.

  11. #11
    Join Date
    Apr 2004
    Posts
    158

    Re: How do I make this work in vb.net?

    Yeehee!! I have done it, and it works like a charm

    Thank you so much for all of you who took time to help, it is very much appreciated.

    Now that I have learned to load and save I can move on to other heroic tasks.

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I make this work in vb.net?

    Let me quote Neil Armstrong :

    "One small step for Dajunka; one giant leap for developers"

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

Featured