file exists

akim

Member
Joined
Jul 6, 2003
Messages
10
Location
germany
hi,

i dont know how to check if a file exists in a directory

my code to open a file is:
Dim sr As IO.StreamReader = New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open))

before i must check if the file exists!!!!!

how can i do this???

can anyone give me an example for my example????

thanx alot

akim
 
Code:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim f As IO.File
        Select Case f.Exists("C:\test.txt")
            Case True
                MessageBox.Show("file exsists!")
            Case False
                MessageBox.Show("file not found!")
        End Select
    End Sub
 
hi,

thx for your answer!!!!!!!!!!!

But can you give me an example that fits with my code????

Dim sr As IO.StreamReader = New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open))

dim a as String = sr.ReadToEnd()

sr.close()



thx,

akim
 
The example dynamic_sysop gave you fits perfectly into your case :)
Code:
If Io.File.Exists("C:\test.txt") Then
     Dim sr As IO.StreamReader = New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open)) 
     dim a as String = sr.ReadToEnd()
     sr.close()
Else
     File does not exit, exit the sub or whatever you want to do
End If
 
hi,

thanx for your example.

but i get the error message that the objectreference is not reverenced to the object instance!!!!!!!!!

in line if textfile.Exists("C:\tex.txt") then

can you hlep me


akim
 
It supposed to be Io.File.Exist not textfile.Exists like you just said, and what I showed you works. You dont need to declare any variable as the File class has shared methods.
:)
 
Back
Top