C# OpenFileDialog 锁定目录

发布于 2024-08-08 17:29:03 字数 163 浏览 2 评论 0原文

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

请告诉我

谢谢

I am making a software that needs to ONLY be able allow people to select files and folders using the OpenFileDialog that are in the same directory as the program and that are in deeper folders. I don't want the OpenFileDialog to be able to select stuff outside of the program's current directory. Is this possible to do in C# using the OpenFileDialog?

Please let me know

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独岁月 2024-08-15 17:29:03

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

将 InitialDirectory 属性设置为您的程序路径。然后,如果用户选择程序路径之外的特定路径,请使用 FileOk 事件来检查这一点并将其返回到 InitialDirectory。

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

I don't see any out of the box support by the OpenFileDialog Control. However, you can try the following,

Set the InitialDirectory property to your program path. Then if a user selects a particular path outside of your program path, use the FileOk event to check this and bring him back to the InitialDirectory.

If you want much more control then you will have to write your custom dialog.

—━☆沉默づ 2024-08-15 17:29:03

我就是这样做的。

   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();

            }                       
        }

This is how I did it.

   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();

            }                       
        }
别想她 2024-08-15 17:29:03

您可以在选择后检查路径是否正确,

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

you can check if the path is correct after selected

if its just accept or send message box tell him you select different directory

眼藏柔 2024-08-15 17:29:03

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

I'm afraid you can't. Most people created their own custom dialog for this scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文