当使用Windows XP中打开文件对话框为什么当前工作目录的变化?对话框、文件、目录、工作

2023-09-04 01:01:12 作者:你加上我等于全世界 。

我发现了一个奇怪的现象,当使用在C#中打开文件对话框。

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

如果使用这种code。在的Windows XP 当前工作目录更改所选文件的路径,但是如果您运行此code。在 Windows 7的当前工作目录不会改变。

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);
    }

任何人都知道这种现象的原因是什么?为什么要在XP中的当前目录的变化,而不是在 Windows 7的

推荐答案

根据您的描述它听起来像的RestoreDirectory属性是XP和Windows7之间的不同。我不知道为什么会出现这种情况,但你可以通过明确设置的值在code解决这个问题。它设置为true,将恢复在对话框关闭的目录。

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.