我正在使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
-Craig
首先,我很抱歉花了这么长时间才理解你的问题。
当我尝试这样做时,我看到的消息是:
无法对“Libraries\Documents”进行操作,因为它不是文件系统的一部分。
没什么可说的了。库是一个虚拟文件夹,它是各种不同的真实文件夹的合并。
没有真正的方法可以避免这个错误。您已要求对话框返回文件夹,但用户尚未选择文件夹。因此,对话无法履行其协议的一部分。
如果您进一步深入文件夹结构,进入真正的文件夹,那么该对话框将返回一个实际值。