|
-
May 14th, 2010, 09:34 AM
#1
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!!!
-
May 14th, 2010, 11:43 AM
#2
Re: OpenFileDialog
You can hardcode it using the property....or are you wanting to make it user configurable?
===============================
My Blog
-
May 14th, 2010, 12:15 PM
#3
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.
-
May 14th, 2010, 12:53 PM
#4
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/
-
May 14th, 2010, 01:27 PM
#5
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???
-
May 14th, 2010, 01:58 PM
#6
Re: OpenFileDialog
 Originally Posted by balexei
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/
-
May 14th, 2010, 03:59 PM
#7
Re: OpenFileDialog
@BigEd781
Agreed. Also, thanks for pointing that out on the snippet.
-
May 15th, 2010, 12:41 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|