【问题标题】:C# OpenFileDialog Lock To DirectoryC# OpenFileDialog 锁定到目录
【发布时间】:2009-10-28 08:29:25
【问题描述】:

我正在制作一个软件,它只需要允许人们使用 OpenFileDialog 选择与程序位于同一目录中且位于更深文件夹中的文件和文件夹。我不希望 OpenFileDialog 能够选择程序当前目录之外的内容。这可以在 C# 中使用 OpenFileDialog 完成吗?

请告诉我

谢谢

【问题讨论】:

  • 从'openfiledialog lock to directory tree'重新标记
  • 我知道这些对话框可以被覆盖和自定义.. 看看这个

标签: c# .net directory-structure openfiledialog


【解决方案1】:

我没有看到 OpenFileDialog 控件提供任何开箱即用的支持。但是,您可以尝试以下方法,

将 InitialDirectory 属性设置为您的程序路径。然后,如果用户选择了您的程序路径之外的特定路径,请使用 FileOk 事件进行检查并将他带回 InitialDirectory。

如果您想要更多的控制,那么您将不得不编写您的自定义对话框。

【讨论】:

    【解决方案2】:

    我就是这样做的。

       openFileDialog1.InitialDirectory = Path.Combine(Path.GetDirectoryName(Application.StartupPath), "FolderName");
    
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {            
                    while(Path.GetDirectoryName(openFileDialog1.FileName) != Path.Combine(Path.GetDirectoryName(Application.StartupPath), "FolderName")){
    
                        MessageBox.Show("Please select .EXE which is in the default folder", "Wrong folder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    openFileDialog1.ShowDialog();
    
                }                       
            }
    

    【讨论】:

      【解决方案3】:

      选中后可以检查路径是否正确

      如果它只是接受或发送消息框告诉他你选择不同的目录

      【讨论】:

        【解决方案4】:

        恐怕你不能。大多数人为此场景创建了自己的自定义对话框。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-09
        • 1970-01-01
        相关资源
        最近更新 更多