Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the current working directory changes when use the Open file dialog in Windows XP?

I have found an strange behavior when use the open file dialog in c#.

If use this code in Windows XP the current working directory changes to the path of the selected file, however if you run this code in Windows 7 the current working directory does not change.

    private void button1_Click(object sender, EventArgs e)
    {            
        MessageBox.Show(string.Format("Current Directory {0}",Directory.GetCurrentDirectory()), "My Application",MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog and get result.
        if (result == DialogResult.OK) 
        {

        }
        MessageBox.Show(string.Format("Current Directory {0}", Directory.GetCurrentDirectory()), "My Application", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }

Anybody know the reason for this behavior? Why the current directory changes in XP and not in Windows 7?

like image 850
RRUZ Avatar asked Jun 10 '10 22:06

RRUZ


1 Answers

Based on your description it sounds like the default value of the RestoreDirectory property is different between XP and Windows7. I'm not sure why this would be the case but you can fix this problem by explicitly setting the value in your code. Setting it to true will restore the directory on dialog close.

like image 116
JaredPar Avatar answered Sep 30 '22 08:09

JaredPar