C# OpenFileDialog 锁定目录
我正在制作一个软件,只需允许人们使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我没有看到 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.
我就是这样做的。
This is how I did it.
您可以在选择后检查路径是否正确,
如果它只是接受或发送消息框告诉他您选择不同的目录
you can check if the path is correct after selected
if its just accept or send message box tell him you select different directory
恐怕你不能。大多数人为此场景创建了自己的自定义对话框。
I'm afraid you can't. Most people created their own custom dialog for this scenario.