- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我将 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/
我一直在尝试在远程服务器上使用 InitialDirectory 但没有成功。我想做的是:(comboBox1 有服务器名称) OpenFileDialog ofLogFile = new OpenF
dialog 是一个 OpenFileDialog 类对象,我正在使用 ShowDialog() 方法。 当我使用包含相对路径的路径时,例如: dialog.InitialDirectory = ".
我有这个代码: OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = GetDataPath(...); di
我想为 Initial directory 赋予适当的值,这样它就会打开我在项目中创建的文件夹(称为“Images”)。我必须使用相对路径,所以我的程序的运行不依赖于我工作的计算机。但问题是我不知道如
我正在打开一个 SaveFileDialog,其初始目录基于用户定义的路径。在传入并打开对话框之前,我想确保此路径有效。现在我得到了这个: Microsoft.Win32.SaveFileDialog
如何设置打开文件对话框出现的初始目录?我想将其更改为位于我的/bin/Debug 文件夹中的图像文件夹。我就是无法让它工作。 opdPicture.Title = "Choose a Picture"
这有点挑剔,但如果文件存在并且 FileName 和 InitialDirectory 设置正确,为什么不自动选择该文件? 我有一个 OpenFileDialog,其中 FileName 和 Init
我将 Windows API 代码包中的 CommonOpenFileDialog 用作文件夹选择器对话框。我将 InitialDirectory 属性设置为 Environment.GetFolde
我是一名优秀的程序员,十分优秀!