|
-
May 1st, 2007, 02:57 PM
#1
Thread Starter
Lively Member
[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
-
May 1st, 2007, 03:41 PM
#2
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...
-
May 1st, 2007, 04:07 PM
#3
Re: OpenFileDialog Revisited
Ah another Vista thread 
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?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 1st, 2007, 05:18 PM
#4
Re: OpenFileDialog Revisited
I'm all out of harsh but fair retorts. 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
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
Last edited by stimbo; May 1st, 2007 at 05:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|