Seeing if a file exists

devil666

Member
Joined
Dec 2, 2006
Messages
8
Programming Experience
1-3
hello!

I need to see if a text file exists...

can someone give me the code to read a text file, or the equal opposite of this:
VB.NET:
Dim strName As New System.IO.FileStream(Application.StartupPath & "\" & Me.TextBox1.Text & ".txt", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
Dim sw As New IO.StreamWriter(strName)
sw.WriteLine("User Name: " & Me.TextBox1.Text)
sw.WriteLine()
sw.WriteLine("Password: " & Me.MaskedTextBox1.Text)
sw.Close()
sw.Dispose()
MyBase.Close()
 
Not sure what you mean by the opposite of that, but to check if a file exists:
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IO.File.Exists([/SIZE][SIZE=2][COLOR=#800000]"C:\whatever.txt"[/COLOR][/SIZE][SIZE=2]) = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]'do stuff here[/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
use the System.IO.StreamReader to read the file just like you use System.IO.StreamWriter to write the file
 
Devil666, since you're using VS2005 (at least your Primary Platform reports that you do) you can find code snippets for many common function like determining if a file exists by using the Code Snippet feature.
To use Code Snippets: right click in the code window and click Insert Snippet...; you'll be presented with categories of snippets and after double clicking one you'll see the snippets in that category.
 
If IO.File.Exists("C:\whatever.txt") = True Then
Dim strName As New System.IO.FileStream(Application.StartupPath & "\" & Me.TextBox1.Text & ".txt", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
Dim sw As New IO.StreamReader(strName)
sw.ReadLine() as string
sw.ReadLine()
sw.ReadLine() as string
sw.Close()
sw.Dispose()
MyBase.Close()



now it says that the two " as string " thigs ar messed up


error given: expression expected
edit reason-i put the wrong code in...
 
If IO.File.Exists("C:\whatever.txt") = True Then
Dim strName As New System.IO.FileStream(Application.StartupPath & "\" & Me.TextBox1.Text & ".txt", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
Dim sw As New IO.StreamReader(strName)
sw.ReadLine() as string
sw.ReadLine()
sw.ReadLine() as string
sw.Close()
sw.Dispose()
MyBase.Close()



now it says that the two " as string " thigs ar messed up


error given: expression expected
edit reason-i put the wrong code in...

Dim myVariable as String
myVariable = sw.ReadLine()
 
Or use the code snippet and see how easy it is:
VB.NET:
myVariable = My.Computer.FileSystem.ReadAllText(ofd.FileName)
No need to mess with a streamReader explicitly.
 
thank you all, i reall like this site after only 2 days!


Now it just dosent do anything... this is in a button click event, IT ONLY COPYS THE FIRST LINE!!!... im sad :(

If IO.File.Exists(Application.StartupPath & "\" & Me.TextBox1.Text & ".txt") = TrueThen
Dim strName AsNew System.IO.FileStream(Application.StartupPath & "\" & Me.TextBox1.Text & ".txt", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
Dim sw AsNew IO.StreamReader(strName)
Dim myVariable AsString
myVariable = sw.ReadLine()
sw.ReadLine()
sw.ReadLine()
sw.ReadLine()
sw.Close()
sw.Dispose()
'MyBase.Close()
Me.Label3.Text = myVariable
EndIf
 
Last edited:
Is there some reason you don't wish to use the code I posted above?

As to why your code only reads the first line: The method ReadLine of the StreamReader returns a string. If you don't assign it to anything then it isn't stored and is lost. In your code, the first time you call the readLine method you assign it to the variable but not for subsequent calls. Look through the methods of the streamReader, you'll find one that returns the whole file.

Or even easier, use the code that I posted above. :)
 
i did try the code you posted above, but it didnt work, if you would like to explain how to use it more in depth, please feel free to pm or post here!
 
Like Paszt said, if you use the ReadAllText(filename) method, you can read the entire file into your variable. Then you can use a split command to separate the lines.

VB.NET:
Dim myLines() as String = Split(myVariable, Environment.NewLine)

If you really want to step through the file, you need to use a loop. Read a line, process the line (look for data, write it out to a new file, etc.) then read another line until the file is empty.

VB.NET:
    Dim inFile As System.IO.File, origFile As System.IO.StreamReader
    Dim BitBucket As String = ""
    origFile = inFile.OpenText(cdMain.FileName)
    Do While origFile.Peek > -1
        BitBucket = origFile.ReadLine
        
        'do something with the line 
        'you now have stored in BitBucket
        Console.WriteLine(BitBucket)
    Loop
    origFile.Close()
 
i did try the code you posted above, but it didnt work, if you would like to explain how to use it more in depth, please feel free to pm or post here!
In order for us to help you we need more information than "it didn't work". Why didn't it work? What error, if any, did you receive?
To use the code I posted, type or copy and paste it into the appropriate method :). You'll have to replace 'myVariable' with the variable you are using.
 
This may help

Dim FILE_NAME As String = ("c:\whatever.txt")
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)

The (FILE_NAME, False) checks to see if the file is there and if its not it creates it.
 
Back
Top