[RESOLVED] OpenFileDialog Revisited
OK Stimbo, i'm ready for you to beat me up again.
This is what I have, during debugging it works just fine. But when I publish it and install it from CD-Rom i keep getting:
Unhandled exception, cannot find file c:\users\name\docs\openfiledialg.
Code:
Dim strList As String
Dim mystreamreader As New StreamReader(OpenFileDialog1.FileName, True)
OpenFileDialog1.InitialDirectory = "c:\temp\"
OpenFileDialog1.Title = "Select a File"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.FileName = "myfile"
If OpenFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
lbxInput.Items.Clear()
strList = mystreamreader.ReadLine()
Do Until strList Is Nothing
lbxInput.Items.Add(strList)
strList = mystreamreader.ReadLine()
Loop
mystreamreader.Close()
mystreamreader.Dispose()
gbxRandom.Enabled = True
gbxResults.Enabled = True
End If
Re: OpenFileDialog Revisited
The problem is that you read the filename from openfiledialog1 BEFORE displaying it... Just think about it... To get the user-chosen file, you have to show him/her the openfiledialog so that he/she can browse for a file. Once the user has selected a file and clicked OK/Open, the openfiledialog will contain the information about that file... And that's when you read the filename...
Re: OpenFileDialog Revisited
Ah another Vista thread :D
Where are you initializing your mystreamreader? Setting the FileName before displaying the dialog is ok as it just sets the initial selection first.
Are you selecting the file from a valid location? Your own user profile?
Re: OpenFileDialog Revisited
I'm all out of harsh but fair retorts. :rolleyes: It appears at that you may be making the exact same mistake as the last time. I see beating you up didn't do any good :D
Quote:
The main problem is your logic. You create a StreamWriter with a path before they have chosen where to Save it. That's what's really causing problems. Declare and use the Streamwriter once the checks for the OK button have been done and the fileName field isn't empty (see below).
http://www.vbforums.com/showpost.php...9&postcount=10