var dlgs = new System.Windows.Forms.OpenFileDialog();
dlgs.CustomPlaces.Clear();
var ListDrives = DriveInfo.GetDrives();
foreach (DriveInfo Drive in ListDrives)
{
if ((Drive.DriveType == DriveType.Fixed) && (Drive.Name != "C"))
{
dlgs.CustomPlaces.Add(Drive.Name);
}
dlgs.ShowDialog();
}我正在尝试打开一个不应该访问本地驱动器C的文件浏览器,以便用户可以选择文件夹是其他本地驱动器中的文件,如("D“,"E")。
发布于 2016-03-25 17:57:45
我现在只是在看OpenFileDialogue class documentation,但我没有看到任何会限制用户使用某些驱动器的东西……This post让我好奇,它是否真的可以做到;但也许它可以使用filter来完成……
发布于 2016-03-25 18:26:34
不可能限制用户在对话框本身中可以访问的位置(除非您实现了自己的对话框)。
但是,可以使用FileOk事件限制文件是否可以打开(是否按下Open按钮或双击将实际关闭对话框)。
类似于:
void DialogFileOk(object sender, CancelEventArgs e)
{
var dialog = sender as OpenFileDialog;
if(dialog == null) return;
if(Path.GetPathRoot(dialog.FileName) == @"C:\")
{
e.Cancel = true;
// maybe show messagebox or task dialog informing the user?
}
}同样,这不会阻止用户浏览C:\驱动器,它只是阻止对话框选择该驱动器中的文件。
PS:如果需要,可以适应多项选择。
https://stackoverflow.com/questions/36217588
复制相似问题