做这样的事情:
// save your current directory
string currentDirectory = Environment.CurrentDirectory;
// create an OpenFileDialog and set RestoreCurrentDirectory to false.
OpenFileDialog ofd = new OpenFileDialog();
ofd.RestoreCurrentDirectory = false;
ofd.ShowDialog();
// save the selected directory locally.
string selectedDirectory = Environment.CurrentDirectory; // OpenFileDialog changed this value.
Environment.CurrentDirectory = currentDirectory; // reset the property with the first value.
// next time you open an OpenFileDialog, set the InitialDirectory property
OpenFileDialog ofd2 = new OpenFileDialog();
ofd.InitialDirectory = selectedDirectory; // set the InitialDirectory to what it was last time an OpenFileDialog was opened.
ofd.ShowDialog();
RestoreDirectory 属性确保 Environment.CurrentDirectory 中的值将在 OpenFileDialog 关闭之前重置。如果 RestoreDirectory 设置为 false,则 Environment.CurrentDirectory 将设置为 OpenFileDialog 上次打开的目录。