Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted
   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

Posted

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

  • *Experts*
Posted

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

Posted

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

  • *Experts*
Posted

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.

:)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...