如何通过单个WinForm对话框获取文件夹或文件路径? [英] How can I get a folder or file path through a single WinForm dialog?

查看:100
本文介绍了如何通过单个WinForm对话框获取文件夹或文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:似乎我不清楚我的问题是什么。约翰·阿伦(John Arlen)对我的标题的编辑也似乎引起了更多的误解。标题更改为如何获取文件夹或文件路径?这不是我以后的事情。我知道有一个对话框可以与文件一起使用,另一个与文件夹一起使用。我知道这些对话框中的每一个都可以返回文件夹或文件的路径。我说我不知道​​如何获取文件路径,但即使我知道如何这样做也没有帮助我。

UPDATE: It seems I wasn't clear in what my problem was. John Arlen's edit to my title also seems to be causing more of a misunderstanding. The title was changed to "How can I get a folder or file path?" which is not what I'm after. I understand that there is a dialog that works with files and another that works with folders. I know that each of these dialogs can return a path of either a folder or file. I stated that I didn't know exactly how to get a file path, but it didn't help me even if I did know how to do so.

如所述在我的原始问题中:

As stated in my original question:


我正在让用户通过一些对话框选择一个文件夹或文件,我不知道是否路径将属于文件夹或文件,一旦用户点击打开按钮,我希望当前选择的目录或文件路径存储在一个字符串中。

"I'm having the user select a folder or file through some dialog. I don't know whether the path will belong to a folder or a file. Once the user hits the 'Open' button, I want the currently selected directory or file path to be stored in a string."

我的意思是我想使用一些可能存在或可能不存在的对话框。我不知道用户是否在文件或文件夹之后。用户知道这一点,但用户不知道路径。这就是为什么使用对话框。用户将搜索所需的文件或文件夹,然后单击打开。为了满足我的需要,可以使用单个对话框来更有意义。我想看看是否存在这样的对话框,因为我对.NET Framework的经验是有限的。

What I meant here was that I wanted to use some dialog that may or may not exist. I don't know if the user is after a file or folder. The user knows this, but the user does not know the path. This is why a dialog is used. The user will search for the file or folder needed, then click "Open". It makes more sense for a single dialog to be used for this for my needs. I wanted to see if such a dialog existed as my experience with the .NET Framework is limited.

Jared Kells的答案几乎正是我正在寻找的。在阅读了他提供的内容之后,似乎没有这样的对话框。我将不得不提供自己的实现。

Jared Kells's answer was almost exactly what I was looking for. After reading what he provided, it seems that such a dialog does not exist. I will have to provide my own implementation.

自从实现我自己的实现可能是耗时和困难的,我现在要做的没有。如果有人提供了非常有用的答案,我会等待几天来选择一个答案。

Since coming up with my own implementation will likely be time consuming and difficult, I'm going to do without for now. I'll wait a couple days to choose an answer in the case that someone provides an exceptionally helpful answer.

感谢那些贡献的人,即使它不是什么

Thanks to those who contributed even if it wasn't quite what I was after.

原始内容:

我正在寻找一个获取文件夹或文件的文件路径的方式。我已经玩了 OpenFileDialog FolderBrowserDialog 没有太多的成功。我可以使用FolderBrowserDialog.SelectedPath获取文件夹路径。使用 OpenFileDialog 类,我无法弄清楚如何获取文件路径。

I'm looking for a way to obtain the file path of a folder or file. I've played around with OpenFileDialog and FolderBrowserDialog without much success. I was able to get the folder paths using FolderBrowserDialog.SelectedPath. Using the OpenFileDialog class, I wasn't able to figure out how to get the file path.

即使我可以想出来,我还在束缚。我让用户通过一些对话框选择一个文件夹或文件。我不知道路径是否属于文件夹或文件。一旦用户点击打开按钮,我希望将当前选择的目录或文件路径存储在一个字符串中。似乎我使用的每个类都被卡住了文件或文件夹。

Even if I could figure that out, I'm still in a bind. I'm having the user select a folder or file through some dialog. I don't know whether the path will belong to a folder or a file. Once the user hits the "Open" button, I want the currently selected directory or file path to be stored in a string. It seems like each of those classes I used are stuck with either files or folders.

WinForms对话框是否可能?我不想在这个时候自己编写对话框。

Is this possible with WinForms dialogs? I'd prefer not having to write my own dialog at this time.

推荐答案

openFileDialog1.ShowDialog();
string filePath = openFileDialog1.FileName;

将给您一个文件的路径。

Will give you the path for a file.

folderBrowserDialog1.ShowDialog();
string folderPath = folderBrowserDialog1.SelectedPath;

一个文件夹。

string path = ...
if(File.Exists(path))...//is file
if(Directory.Exists(path))...//is folder

查看是什么。

这篇关于如何通过单个WinForm对话框获取文件夹或文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆