Default Dialog Directories
-
Hey everyone, This may be a very simple (hopefully it is) question and answer, but I'm having trouble figuring this one out. I want a SaveFileDialog and an OpenFileDialog to start in a chosen directory. Seems simple enough, I'll just create the directory in case it doesn't exist, and then set the InitialDirectory property of the dialog -- but for some reason it's still opening in the last directory that I had browsed to in a previous dialog. Here's the code:
private const string ATF_FILTER = "AOI Template Files (*.atf)|*.atf"; private const string C_SAVE_DIRECTORY = @"./Saved Templates"; //Save Template private void Save_Click(object sender, EventArgs e) { Directory.CreateDirectory(C_SAVE_DIRECTORY); SaveFileDialog diag = new SaveFileDialog(); diag.InitialDirectory = C_SAVE_DIRECTORY; diag.DefaultExt = ".atf"; diag.Filter = ATF_FILTER; if (diag.ShowDialog() == DialogResult.OK) { DrawBox.SaveTemplate(diag.FileName); } }
Any idea what could be going wrong? Thanks, Phil
-
Hey everyone, This may be a very simple (hopefully it is) question and answer, but I'm having trouble figuring this one out. I want a SaveFileDialog and an OpenFileDialog to start in a chosen directory. Seems simple enough, I'll just create the directory in case it doesn't exist, and then set the InitialDirectory property of the dialog -- but for some reason it's still opening in the last directory that I had browsed to in a previous dialog. Here's the code:
private const string ATF_FILTER = "AOI Template Files (*.atf)|*.atf"; private const string C_SAVE_DIRECTORY = @"./Saved Templates"; //Save Template private void Save_Click(object sender, EventArgs e) { Directory.CreateDirectory(C_SAVE_DIRECTORY); SaveFileDialog diag = new SaveFileDialog(); diag.InitialDirectory = C_SAVE_DIRECTORY; diag.DefaultExt = ".atf"; diag.Filter = ATF_FILTER; if (diag.ShowDialog() == DialogResult.OK) { DrawBox.SaveTemplate(diag.FileName); } }
Any idea what could be going wrong? Thanks, Phil
-
Hey everyone, This may be a very simple (hopefully it is) question and answer, but I'm having trouble figuring this one out. I want a SaveFileDialog and an OpenFileDialog to start in a chosen directory. Seems simple enough, I'll just create the directory in case it doesn't exist, and then set the InitialDirectory property of the dialog -- but for some reason it's still opening in the last directory that I had browsed to in a previous dialog. Here's the code:
private const string ATF_FILTER = "AOI Template Files (*.atf)|*.atf"; private const string C_SAVE_DIRECTORY = @"./Saved Templates"; //Save Template private void Save_Click(object sender, EventArgs e) { Directory.CreateDirectory(C_SAVE_DIRECTORY); SaveFileDialog diag = new SaveFileDialog(); diag.InitialDirectory = C_SAVE_DIRECTORY; diag.DefaultExt = ".atf"; diag.Filter = ATF_FILTER; if (diag.ShowDialog() == DialogResult.OK) { DrawBox.SaveTemplate(diag.FileName); } }
Any idea what could be going wrong? Thanks, Phil
Hi, it depends on FileDialog.RestoreDirectory which by default is false, but it seems you want it to be true. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
PhilDanger wrote:
Any idea what could be going wrong?
Relative path perhaps? As someone pointed out in the last few days, best practice is to use absolute paths.
Turns out it works correctly when I switch it from a forward slash to a backslash: private const string C_SAVE_DIRECTORY = @".\Saved Templates"; //works! Could you elaborate a bit on the benefits of using an absolute path over a relative path? My intuition and previous web experience tells me that using an absolute path would not be the best solution, but perhaps times have changed. Thanks, Phil
-
Turns out it works correctly when I switch it from a forward slash to a backslash: private const string C_SAVE_DIRECTORY = @".\Saved Templates"; //works! Could you elaborate a bit on the benefits of using an absolute path over a relative path? My intuition and previous web experience tells me that using an absolute path would not be the best solution, but perhaps times have changed. Thanks, Phil
PhilDanger wrote:
benefits of using an absolute path over a relative path?
Well they are "absolute". Relative is subject to the Current Directory so it's only as good as a guarantee that it hasn't changed or is not something other than what you expect it to be.