CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: OpenFileDialog

  1. #1
    Join Date
    Jan 2002
    Posts
    87

    OpenFileDialog

    Dear Code Gurus,

    I am using the OpenFileDialog and was wondering if there is any easy way to save the InitialDirectory, so that when the program is closed and then restarted again it could read this InitialDirectory from somewhere when this dialog is used (in the new instance of the program).

    Thank you for your help!!!

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: OpenFileDialog

    You can hardcode it using the property....or are you wanting to make it user configurable?
    ===============================
    My Blog

  3. #3
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Wink Re: OpenFileDialog

    Hello :-

    As an example, you can use InitialDirectory - to set your open directory...

    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    static class MainClass
    {
        [STAThread]
        static void Main()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "RichText Files (*.rtf)|*.RTF|Text Files (*.txt)|*.TXT" +
              "|All files (*.*)|*.*";
            dlg.CheckFileExists = true;
            dlg.InitialDirectory = Application.StartupPath;
    
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(dlg.FileName);
            }
      
        }
    }
    Now, the neat aspect is, if you use FileInfo to store your searched Dialog location,
    then you can use the properties of the FileInfo object to retrieve the
    information you want:

    Code:
    FileInfo fi = new FileInfo(dlg.FileName);
    ...
    fi.DirectoryName \\ the directory's full path
    fi.Name \\ the file name
    fi.Extension \\ the file extension
    Hope this helps!
    -Quinn

    If this helps, please rate this post!
    Last edited by QuinnJohns; May 14th, 2010 at 12:24 PM.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: OpenFileDialog

    @ QuinnJohns: I realize that you were only providing an example, but you are not disposing of the dialog. I would do it this way instead:

    Code:
    using( SaveFileDialog dlg = new SaveFileDialog() )
    {
        dlg.Filter = "RichText Files (*.rtf)|*.RTF|Text Files (*.txt)|*.TXT|All files (*.*)|*.*";
        dlg.CheckFileExists = true;
        dlg.InitialDirectory = Application.StartupPath;
    
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            Console.WriteLine(dlg.FileName);
        }
    }
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Jan 2002
    Posts
    87

    Re: OpenFileDialog

    Thank you for the replies!!! But it is not exactly what I am looking for, perhaps I have not explained the issue very well. The InitialDirectory of my OpenFileDialog should not be connected to the path for the executable. It should be connected to the path that the program used during the previous execution, i.e. I opened the file using the OpenFileDialog, did something with it and than closed the whole application.

    Upon restarting the application (i.e. executing the executable for the second time) I want the InitialDirectory to be initialized to what I used before.

    Any ideas???

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: OpenFileDialog

    Quote Originally Posted by balexei View Post
    Thank you for the replies!!! But it is not exactly what I am looking for, perhaps I have not explained the issue very well. The InitialDirectory of my OpenFileDialog should not be connected to the path for the executable. It should be connected to the path that the program used during the previous execution, i.e. I opened the file using the OpenFileDialog, did something with it and than closed the whole application.

    Upon restarting the application (i.e. executing the executable for the second time) I want the InitialDirectory to be initialized to what I used before.

    Any ideas???
    Just save it in a configuration file or the registry...
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  7. #7
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: OpenFileDialog

    @BigEd781

    Agreed. Also, thanks for pointing that out on the snippet.

  8. #8
    Join Date
    Jan 2002
    Posts
    87

    Re: OpenFileDialog

    Thank you for your help!!!

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

Featured