Question : VB.net 2008 Close an OpenRead

I have a program that is reading the contents of a directory and populating a combo box with the names of every sql file.

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
For Each f As FileInfo In folder.GetFiles
            Try
                'try to open each file for read
                f.OpenRead()
                'f.openRead.close
                'f.openRead.dispose
                'catch error
            Catch c As Exception
                'permissions issue, skip file
                GoTo nxt2
            End Try
            'check if file is a sql file
            If f.Name.Substring(Len(f.Name) - 4, 4) = ".sql" Then
                'file is a sql file
                cmbscript.Items.Add(f.Name.Replace(".sql", ""))
            End If
nxt2:

        Next


I'm using OpenRead()

How do I close it after the combobox is populated with the name of the file?  Even after the sub is done I can't edit the file or change it's name.  It shouldn't be keeping an open connection to it.

I've tried:
f.openread().close
f.openread().dispose

neither work

What am I doing wrong?

Answer : VB.net 2008 Close an OpenRead

Try something like:
1:
2:
3:
4:
5:
6:
7:
8:
        For Each f As FileInfo In folder.GetFiles("*.sql")
            Try
                Using fs As FileStream = f.OpenRead()
                End Using
                cmbscript.Items.Add(Path.GetFileNameWithoutExtension(f.Name))
            Catch c As Exception
            End Try
        Next
Random Solutions  
 
programming4us programming4us