Results 1 to 4 of 4

Thread: [RESOLVED] OpenFileDialog Revisited

  1. #1

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Resolved [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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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...

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width