We help IT Professionals succeed at work.

read a text file and copy value  into vb.net code

wmbentoniii
wmbentoniii asked
on

hi,
 how can I read a text file and copy the value  into a vb.net  2008 code. for example  I have
a simple text file  named test.txt ( C:\Test.text ) that will have one line of data  to read  which is (server)

 I need to take the word server  in the text file and place it where the null is at in code below so that when I run vb code it sees server instead of null and then clears itself out for the next text

thanks
Set mastObj = stnObj.Masters("NULL")
Comment
Watch Question

Top Expert 2010

Commented:
Hi,
If you need to only read the first line try using this approach. If the call allows this I assume it wants a string?
Usage:
Set mastObj = stnObj.Masters(GetFirstLineFromFile("e:\test.txt"))

Public Function GetFirstLineFromFile(ByVal filePath As String) As String
        Dim lineData As String = String.Empty
        Dim fs As New FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        Dim tr As TextReader
        fs.Position = 0
        tr = New StreamReader(fs)
        TextReader.Synchronized(tr)
        lineData = tr.ReadLine()
        fs.Dispose()
        tr.Dispose()
        Return lineData
    End Function

Open in new window

Top Expert 2010

Commented:
Whoops!.. Change line 7 to the following.
 

tr = TextReader.Synchronized(tr)

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
How about just?

    Set mastObj = stnObj.Masters(System.IO.File.ReadAllLines("C:\Test.text")(0))
Top Expert 2010

Commented:
That is short and sweet Idle, I think it all depends how you want to work with the file and what access you want to offer. I prefer a little more control but your example probrably will be accepted. lol
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
True...it's got no error checking and no sharing mode involved at all...  =\
Top Expert 2010

Commented:
That's the cool thing about .NET there is so many ways to do stuff. I like your approach and it probrably suits the OP more for simplicity. What I don't understand is why they don't give more information in the documents that tell you how exactly that opens the file. I can guess and say it uses full share access but why do we have to actually test it it would be nice if they give more information on some of the IO methods.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You could use Reflector and see what it gets changed to "under the hood"...  =)

Author

Commented:
great  job it worked fantatisc, but I have one more question for you :-)

So what would happen if you need to read a second or third line in the same text file? Would you need to duplicate the code or something else. Example

Star
Square
Server
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You can do:

    Dim lines() As String = System.IO.File.ReadAllLines("C:\Test.text")

Then you can access them by index:

    Debug.Print(lines(0)) ' first line
    Debug.Print(lines(1)) ' second line
    Debug.Print(lines(2)) ' third line
    ' etc...

Author

Commented:
When I run the code with I get Lines is a type not an expresion, should I put them in quotes? (" Lines")  
mastObj1 = stnObj.Masters(System.IO.File.ReadAllLines("C:\test.txt")(0))
Debug.Print(lines(0))  
Debug.Print(lines(1))  
Thanks :-)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You need to do:

    Dim lines() As String = System.IO.File.ReadAllLines("C:\Test.text")
    mastObj1 = stnObj.Masters(lines(0))
    Debug.Print("Second Line: " & lines(1))  
    Debug.Print("Third Line: " & lines(2))