How can I get a folder or file path through a sing

2019-04-28 09:00发布

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."

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'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.

ORIGINAL CONTENT:

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.

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

2条回答
走好不送
2楼-- · 2019-04-28 09:29

See this question for some methods of displaying a common dialog that can select both files and folders.

Select either a file or folder from the same dialog in .NET

Based on answers to the question above I have created a simple example of how to do this in C# here: https://github.com/jkells/folder-browser-dialog-example

You should be able to just copy FolderBrowserDialogEx.cs into your project.

查看更多
一夜七次
3楼-- · 2019-04-28 09:31
openFileDialog1.ShowDialog();
string filePath = openFileDialog1.FileName;

Will give you the path for a file.

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

for a folder.

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

to check what it is.

查看更多
登录 后发表回答