15

Why does OpenFileDialog change my working directory? Should i assume many func in System.Windows.Forms will change my working directory?

    OpenFileDialog open = new OpenFileDialog();
    open.Filter = filter;
    a = Directory.GetCurrentDirectory(); //<-- correct
    if (open.ShowDialog() == DialogResult.OK) //-- select a file on my desktop
    {
        a = Directory.GetCurrentDirectory(); //<-- incorrect, is set to my desktop

5 Answers5

19

Or you can make it not do that. See the FileDialog.RestoreDirectory property.

Mike
  • 206
  • 2
  • 3
9

What we have discovered in a current project is that the OpenFileDialog no longer changes the current directory, making the property .RestoreDirectory obsolete. The code in the application used to change the current directory whenever we opened a file(when running in Windows XP). It no longer does that in Windows 7. As a result it broke our application because now our dataset does not know which directory the file is in when we attempt to deserialize it by using the filename without the full path. Just a word of caution if you plan to migrate to windows 7.

FernandoZ
  • 439
  • 6
  • 13
  • 2
    good lord, I just found this comment after having fought with this crazyness for about 3 hours, trying to understand why the heck I was having different behaviors on XP and windows 7 for a freaking file dialog. – Galactus Feb 15 '12 at 22:15
7

The current working directory can change during runtime, yes.

Consider using

Directory.GetParent(Assembly.GetExecutingAssembly().Location)

or

System.AppDomain.CurrentDomain.BaseDirectory

when you need your applications directory.

tanascius
  • 53,078
  • 22
  • 114
  • 136
  • heh, not that easy. I just use get/set when i need to. I use MSVS to set the working directory so theres no possible way for me to detect where it should be (unless i hardcode it into the exe) –  May 31 '09 at 03:45
7

It is a pain, although in some ways you might anticipate it... if you go into an open dialog multiple times (in an app) you often find it where you last left it.

If it impacts your code, you could take a snapshot of GetCurrentDirectory() before going into the dialog, and restore it afterwards (so your code doesn't see the change). You might want to store the user's working directory separately (and swap them) so that the user also gets their expected behaviour.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thats exactly what i did. It was just surprising to see it happened. I am glad to see you think it is a pain as well. –  May 31 '09 at 03:44
  • That the OpenFileDialog opens the same folder the next time you go into it in the same app has nothing to do with the fact that it sets the current directory. It is some windows build in magic to remember the last directory for those dialogs (probably somewhere in the registry). – ChrisWue Jul 03 '12 at 22:32
0

for Why in XP the filedialog change the current directory , it's better to ask MS. anyway the open file dialog in XP has this strange behavior ,but in w7 or higher not. so you can simply set the current directory after you saved the path selected from SaveFileDialog taht it change the current directory.

I post my method where you can see that the path chosed is saved to settings and reset the current directory

 private void ShowSaveFileDialog(object sender, RoutedEventArgs e)
    {    
        private const int xpVerMajorNumber = 5;        
        var saveFileDialog = new SaveFileDialog()
        {                
            FileName = Settings.Default.ExcelFileName,
            DefaultExt = "*.xlsx",
            Filter = "Excel Workbook (.xlsx)|*.xlsx"
        };

        if (saveFileDialog.ShowDialog(this) == true)
            Settings.Default.ExcelFileName = saveFileDialog.FileName;


         if (Environment.OSVersion.Version.Major <= xpVerMajorNumber)
        {
       Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
        }

    }
luka
  • 605
  • 8
  • 15
  • This does not provide answer to the question. OP wanted to know **WHY** – lokusking Jul 01 '16 at 09:56
  • 1) The other answers said all that needed to be said 2) XP is no longer as support OS 3) The question is 7 years old why are you answering! –  Jul 01 '16 at 21:25
  • right to contribute, is not the purpose of this site? yes I'know that xp is no longer as support, but the 90% of machine industry still have an xp o.s. mounted. – luka Jul 02 '16 at 07:17