Reading Text files and learning about VS 2005


 


Recieved this Query on one of the alia’s i am on and thought other folks might be interested in the answer:


 


Hi guys,



I am learning  how to use VB .Net (haven’t-programmed-for-ten-years-way), as there has been huge interest in it from the students at my unis.



I’m struggling to find a really helpful get started guide. I have the “Build a Program Now!” book and I have looked at the online stuff but I just seem to get into loops or the simple things I want to do eg reading a text file just don’t seem at all obvious.



What I am trying to do (because I’m old school!) is create an application that reads a text file, munges each line and then outputs a file in a scrubbed format. I am getting stuck on inheriting system objects and “can’t do x with y” errors that didn’t exist in VB4!



Any hints about where I could start?!



Thanks,


Mark


 


 


*********************** 


Hello Mark,


I find the Teched hands on labs one the best learning resource we have, you can find a list of them at:


http://blogs.msdn.com/charles_sterling/archive/2005/10/10/478983.aspx


 


 


For your specific question the new VS 2005 code snippets are really helpful (Right click in your code and choose “insert snippet”) the snippet below is from the “File System” group seems pretty close to what you want to do…interestingly I didn’t even know about the TextFieldParser() class before your query!


 


-Chuck


 


Dim filename As String = “C:\Test.txt”


        Dim fields As String()


        Dim delimiter As String = “,”


        Using parser As New TextFieldParser(filename)


            parser.SetDelimiters(delimiter)


            While Not parser.EndOfData


                ‘ Read in the fields for the current line


                ListBox1.Items.Add(parser.ReadFields(0))


                 ‘ Add code here to use data in fields variable.


             End While


        End Using


 


 


 


Greg Low pointed out an even easier method if you don’t need to massage the text:


 


From: Greg Low [mailto:greg.low@readify.net]
Sent: Friday, January 13, 2006 12:10 PM
To: Charles Sterling
Subject: Reading text files blog


Hi Chuck,


or even:


 


        Dim lines() As String = System.IO.File.ReadAllLines(“c:\test.txt”)
        ListBox1.DataSource = lines


HTH,


 

Greg

Dr Greg Low

Readify – Senior Consultant



Unit 206 Nolan Tower,


29 Rakaia Way


Docklands  VIC 3008


M: +61 419 201 410


F: +61 (3) 9600 1665


E: greg.low@readify.net


W: www.readify.net