gpt4 book ai didi

c# - 我的文档路径重定向到 OneDrive 路径

转载 作者:太空宇宙 更新时间:2023-11-03 13:00:03 25 4
gpt4 key购买 nike

我将从非常简单的代码开始



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 设置为运行应用程序的我的文档文件夹的用户。

代码设置Initial Directory的路径为:C:\Users\Aaron\Documents,即测试用户My Documents Directory。

当我运行代码时,OpenFileDialog 实际上在目录中打开:C:\Users\Aaron\OneDrive\Documents。这是 One Drive 的位置。

这发生在我的两台机器上,但不是我 friend 的机器。

为什么 OneDrive 文档文件夹打开时这不是设置为 OpenFIleDialog.InitialDirectory 的路径?

编辑:我应该更新这个。第二天我再次运行我的项目,问题不再发生。我也没有更改我的代码。这一定是侥幸。

最佳答案

对话框不应打开“OneDrive\Documents”。可能是您已将“文档”文件夹重定向到 OneDrive 的文件夹,但由于您或多或少对路径进行了硬编码,这似乎不太可能。

这就是为什么在一般情况下您不应该假设用户的文档位于 C:\Users\{USERNAME}\Documents 中。它可以由用户或组策略更改,并且不能保证在未来版本的 Windows 中存在。

要查找用户的“我的文档”文件夹(或 Vista 及更高版本的“文档”),请使用:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

所以你的代码是:

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

关于c# - 我的文档路径重定向到 OneDrive 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com