OpenFileDialog:打开目录
我希望能够在对话框中选择目录(除了单个文件之外)。现在,当我在选择目录时单击“打开”时,它会下降到目录中,而我希望它返回目录的路径。
另外,我将如何阅读这样的路径?我没有看到任何会返回完整路径的属性。
I'd like to be able to select directories (in addition to single files) in the dialog. As it is now, when I click 'open' when the directory is selected, it descends into the directory, while I want it to return the path to the directory.
Additionally, how would I read such path? I don't see any property that would return the full path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用
相反,FolderBrowserDialog
。该对话框旨在允许用户选择文件夹。如果您愿意,您还可以让用户直接从对话框创建新文件夹。
您不应尝试允许用户从同一对话框中选择文件或文件夹。我想我从来没有在应用程序中见过这样的事情。考虑一下区分扩展目录以查找文件的用户和尝试选择目录本身的用户是多么困难。这就是为什么提供了两个单独的对话框;选择最适合您目的的一种。
至于读取 OpenFileDialog 中所选文件的完整路径,您想要的属性(有点令人困惑)称为
文件名
。该文档解释说:或者,如果您允许多项选择,您可能需要使用
FileNames
属性 代替。The easy way is to use a
FolderBrowserDialog
instead.This dialog is designed to allow the user to select a folder. You can also enable the user to create a new folder, if you like, straight from the dialog box.
You shouldn't try and allow the user to select either a file or a folder from the same dialog. I don't think I've ever seen that done in an application. Consider how difficult it would be to distinguish between users expanding directories to locate files, and users who are attempting to select the directory itself. This is why two separate dialogs have been provided; choose the one that best suits your purposes.
As far as reading the full path to the file selected in an
OpenFileDialog
, the property you want is (somewhat confusingly) calledFileName
. The documentation explains that:Or, if you're allowing multiple selection, you'll likely want to use the
FileNames
property instead.