【发布时间】:2015-09-15 23:55:00
【问题描述】:
我将从非常简单的代码开始
string fileName; // filename of file
// get the filename
using (OpenFileDialog openFileDialog = new OpenFileDialog()) {
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.ShowDialog();
fileName = openFileDialog.FileName;
}
我要做的是使用.Net OpenFileDialog. 并将InitialDirectory 设置为运行应用程序的“我的文档”文件夹的用户。
代码将初始目录的路径设置为:C:\Users\Aaron\Documents,即测试用户我的文档目录。
当我运行代码时,OpenFileDialog 实际上是在目录中打开的:C:\Users\Aaron\OneDrive\Documents。哪个是 One Drive 位置。
这发生在我的两台机器上,但不是我朋友的机器。
当路径未设置为OpenFIleDialog.InitialDirectory 时,为什么会打开 OneDrive 文档文件夹?
编辑:我可能应该更新这个。第二天我再次运行我的项目,问题不再发生。我也没有更改我的代码。这一定是侥幸的情况。
【问题讨论】:
-
旁注:如果你想要用户的文件,你应该使用
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)。文档的路径可以由用户更改,也可以作为组策略的函数。 -
@theB,非常好的观点。对于这个特定的用例;但是,路径是正确的。不过,我将编辑问题以反映这种更好的做法。
-
我真的看不出它为什么会这样做。你试过
Environment.GetFolderPath的方式吗? -
是的,我事先使用 Environment.GetFolderPath 运行了它。无论哪种方式,InitialDirectory 设置的路径都是正确的,但是当对话框打开时,它会在 OneDrive 文档中打开。
-
我无法重现此问题。它似乎特定于您的机器。
标签: c# .net openfiledialog onedrive documents