在c#中使用打开的文件对话框时,我发现了一个奇怪的行为。当在Windows XP中使用打开文件对话框时,为什么当前工作目录更改?
如果在Windows XP
中使用此代码,则当前工作目录将更改为所选文件的路径,但如果在Windows 7
中运行此代码,则当前工作目录不会更改。
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
?
感谢您的回答我真的知道如何解决这个问题。我只想知道这种行为的原因。 – RRUZ 2010-06-10 22:30:06
@RRUZ,我很好奇自己。通过反射器中的代码挖掘,我看到了Vista的一种特殊方法(HandleVistaFileOK)。我的怀疑是这种方法是负责任的,但它再次没有真正回答为什么。 – JaredPar 2010-06-10 22:32:20