Why does OpenFileDialog change my working directory? Should i assume many func in System.Windows.Forms will change my working directory?
为什么OpenFileDialog会改变我的工作目录?我应该假设System.Windows.Forms中的许多func会改变我的工作目录吗?
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 个解决方案
#1
19
Or you can make it not do that. See the FileDialog.RestoreDirectory property.
或者你可以让它不那样做。请参阅FileDialog.RestoreDirectory属性。
#2
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.
我们在当前项目中发现的是,OpenFileDialog不再更改当前目录,使属性.RestoreDirectory过时。应用程序中的代码用于在我们打开文件时(在Windows XP中运行时)更改当前目录。它在Windows 7中不再那样做了。结果它打破了我们的应用程序,因为现在我们的数据集在我们尝试使用没有完整路径的文件名反序列化文件时不知道该文件所在的目录。如果您打算迁移到Windows 7,请注意一点。
#3
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.
如果它影响您的代码,您可以在进入对话框之前拍摄GetCurrentDirectory()的快照,然后再恢复(因此您的代码看不到更改)。您可能希望单独存储用户的工作目录(并交换它们),以便用户也获得其预期的行为。
#4
6
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.
当您需要您的应用程序目录时。
#5
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.
为什么在XP中,filedialog改变当前目录,最好问问MS。无论如何,XP中的打开文件对话框有这种奇怪的行为,但在w7或更高版本中没有。因此,您可以在保存从SaveFileDialog中选择的路径后设置当前目录,以便更改当前目录。
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);
}
}
#1
19
Or you can make it not do that. See the FileDialog.RestoreDirectory property.
或者你可以让它不那样做。请参阅FileDialog.RestoreDirectory属性。
#2
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.
我们在当前项目中发现的是,OpenFileDialog不再更改当前目录,使属性.RestoreDirectory过时。应用程序中的代码用于在我们打开文件时(在Windows XP中运行时)更改当前目录。它在Windows 7中不再那样做了。结果它打破了我们的应用程序,因为现在我们的数据集在我们尝试使用没有完整路径的文件名反序列化文件时不知道该文件所在的目录。如果您打算迁移到Windows 7,请注意一点。
#3
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.
如果它影响您的代码,您可以在进入对话框之前拍摄GetCurrentDirectory()的快照,然后再恢复(因此您的代码看不到更改)。您可能希望单独存储用户的工作目录(并交换它们),以便用户也获得其预期的行为。
#4
6
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.
当您需要您的应用程序目录时。
#5
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.
为什么在XP中,filedialog改变当前目录,最好问问MS。无论如何,XP中的打开文件对话框有这种奇怪的行为,但在w7或更高版本中没有。因此,您可以在保存从SaveFileDialog中选择的路径后设置当前目录,以便更改当前目录。
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);
}
}