gpt4 book ai didi

c# - 如何让 CommonOpenFileDialog 的 InitialDirectory 成为用户的 MyDocuments 路径,而不是 Libraries\Documents?

转载 作者:太空狗 更新时间:2023-10-29 23:41:45 26 4
gpt4 key购买 nike

我将 Windows API 代码包中的 CommonOpenFileDialog 用作文件夹选择器对话框。我将 InitialDirectory 属性设置为 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)。但是,当我显示对话框时,地址栏中的路径是 Libraries\Documents(不是我期望的 C:\users\craig\my documents)。此外,如果我只是按下“选择文件夹”按钮,我会看到一个对话框,提示“您选择了一个库”。请改为选择一个文件夹。'

有人知道为什么我的文件路径被忽略,转而使用“libraries\documents”吗?更重要的是,如何让对话框尊重我传入的 InitialDirectory 值?

我在对话框中使用的代码是:

if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = true;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.AllowNonFileSystemItems = false;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folderSelectorDialog.Title = "Project Location";

if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
ShellContainer shellContainer = null;

try
{
// Try to get a valid selected item
shellContainer = folderSelectorDialog.FileAsShellObject as ShellContainer;
}
catch
{
MessageBox.Show("Could not create a ShellObject from the selected item");
}

FilePath = shellContainer != null ? shellContainer.ParsingName : string.Empty;
}
}

谢谢,

-克雷格

最佳答案

首先,很抱歉我花了这么长时间才理解您的问题。

当我尝试这个时,我看到的消息是:

Cannot operate on 'Libraries\Documents' because it is not part of the file system.

没有更多要说的了。库是一个虚拟文件夹,它是各种不同的真实文件夹的合并。

没有真正的方法可以避免这个错误。您已要求对话框返回文件夹,但用户尚未选择文件夹。因此,对话无法履行其在交易中的职责。

如果您进一步深入文件夹结构,进入真正的文件夹,那么对话框将返回一个真正的值。

关于c# - 如何让 CommonOpenFileDialog 的 InitialDirectory 成为用户的 MyDocuments 路径,而不是 Libraries\Documents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425758/

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