vbMarkO Posted August 23, 2005 Posted August 23, 2005 Ok, Here my code piece..... DOnt I need to close the file here? If so How? Try If File.Exists(frPath) Then Dim rFile As StreamReader = File.OpenText(frPath) ReadString = rFile.ReadToEnd() stubText.Text = ReadString End If ' Q: DO I need to close this File? How? Where? Here or after ' End Try? Catch End Try vbMarkO Quote Visual Basic 2008 Express Edition!
penfold69 Posted August 23, 2005 Posted August 23, 2005 Dim rFile as StreamReader Try If File.Exists(frPath) Then rFile = File.OpenText(frPath) ReadString = rFile.ReadToEnd() stubText.Text = ReadString End If Catch ex as Exception Finally rFile.Close() End Try Quote
Machaira Posted August 23, 2005 Posted August 23, 2005 It's always a good practice to close any files you open. Quote Here's what I'm up to.
vbMarkO Posted August 24, 2005 Author Posted August 24, 2005 Thanx guys.... This one is now resolved your sugestions worked vbMarkO Quote Visual Basic 2008 Express Edition!
jmcilhinney Posted August 24, 2005 Posted August 24, 2005 Dim rFile as StreamReader Try If File.Exists(frPath) Then rFile = File.OpenText(frPath) ReadString = rFile.ReadToEnd() stubText.Text = ReadString End If Catch ex as Exception Finally rFile.Close() End Try This code will throw an NullReferenceException if the file does not exist, because no reference will ever be assigned to the rFile variable but the code will still call Close on it. You would need to test for a null reference in the Finally block:Dim rFile as StreamReader Try If File.Exists(frPath) Then rFile = File.OpenText(frPath) ReadString = rFile.ReadToEnd() stubText.Text = ReadString End If Catch ex as Exception Finally If Not rFile Is Nothing Then rFile.Close() End If End Tryor use two Try...Catch blocks:Dim rFile as StreamReader Try If File.Exists(frPath) Then rFile = File.OpenText(frPath) Try ReadString = rFile.ReadToEnd() stubText.Text = ReadString Catch ex as Exception Finally rFile.Close() End Try End If Catch ex as Exception End Try Quote
vbMarkO Posted August 25, 2005 Author Posted August 25, 2005 Thank you so much :) That did the trick..... Quote Visual Basic 2008 Express Edition!
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.