akim Posted July 6, 2003 Posted July 6, 2003 hi, i don't 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 Quote
Leaders dynamic_sysop Posted July 6, 2003 Leaders Posted July 6, 2003 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 Quote
akim Posted July 6, 2003 Author Posted July 6, 2003 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 Quote
*Experts* mutant Posted July 7, 2003 *Experts* Posted July 7, 2003 The example dynamic_sysop gave you fits perfectly into your case :) 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 Quote
akim Posted July 7, 2003 Author Posted July 7, 2003 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 Quote
*Experts* mutant Posted July 7, 2003 *Experts* Posted July 7, 2003 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. :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.