OpenFileDialog:打开目录

发布于 2024-10-24 07:35:16 字数 115 浏览 13 评论 0原文

我希望能够在对话框中选择目录(除了单个文件之外)。现在,当我在选择目录时单击“打开”时,它会下降到目录中,而我希望它返回目录的路径。

另外,我将如何阅读这样的路径?我没有看到任何会返回完整路径的属性。

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 技术交流群。

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

发布评论

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

评论(1

枉心 2024-10-31 07:35:16

最简单的方法是使用 相反,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) called FileName. The documentation explains that:

The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").

Or, if you're allowing multiple selection, you'll likely want to use the FileNames property instead.

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