OpenFile and SaveFile dialogs change the current directory.
-
I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai
-
I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai
That's how they are expected to behave. Instead of using CurrentDirectory, you can try using
System.AppDomain.CurrentDomain.ApplicationDirectory
or something similar. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai
Your code really shouldn't be depending on the current directory anyway. The general rule-of-thumb in programming is assume nothing!. What this means is you never assume that the file you want to access is in the current directory. Always use fully qualified path names and not just a filename when doing any kind of file access. This also has other benefits. Say you put up a OpenFileDialog that changed the current directory. After the user picks a file, you try and put the current directory back to what it was. But, your code crashes, for whatever reason, before that can happen! The function ends up returning as failed, but not having restored the current directory. Now what? Your next file access is assuming (there's that word again ;)) the current directory is correct when it's not, and your app ends up crashing a second time, then a third, forth, fifth, ... Write your code so it defends itself against itself. Never assume anything is what you expect it to be. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 16:07 Monday 12th September, 2005
-
I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai
Use this:
System.Windows.Forms.OpenFileDialog dlgOpenFile = new System.Windows.Forms.OpenFileDialog(); ... **dlgOpenFile.RestoreDirectory = true;** DialogResult res = dlgOpenFile.ShowDialog();
But listen to dave's answer, he is totaly right about assuming paths.