How can I get the path to a folder or file through a single WinForm dialog?

UPDATE: It seems I didn’t quite understand what my problem was. John Arlen edits my headline and seems to cause more misunderstanding. The name has been changed to "How can I get the path to a folder or file?" and that’s not what I need. I understand that there is a dialog that works with files, and the other with folders. I know that each of these dialogs can return the path to a folder or file. I stated that I did not know exactly how to get the file path, but that did not help me, even if I knew how to do it.

As stated in my original question:

“I have a user who selects a folder or file through some dialogue. I don’t know if the path belongs to the folder or file. When the user clicks the Open button, I want the currently selected directory or path to the file to save to line. "

What I had in mind here is that I wanted to use some kind of dialogue, which may or may not exist. I do not know if the user is after the file or folder. The user knows this, but the user does not know the path. This is why dialogue is used. The user will search for the desired file or folder, then click "Open." For me it makes more sense to use one dialogue for my needs. I wanted to see if such a dialogue exists, since my experience with the .NET Framework is limited.

The answer to the question of Jared Kells was almost exactly what I was looking for. After reading what he provided, it seems that such a dialogue does not exist. I will have to provide my own implementation.

Since coming up with my own implementation is likely to be time-consuming and difficult, I will not manage to do this yet. I will wait a couple of days to select an answer in case someone provides an exceptionally useful answer.

Thanks to those who contributed, even if it was not quite what I was.

ORIGINAL CONTENT:

I am looking for a way to get the file path of a folder or file. I played a lot with OpenFileDialog and FolderBrowserDialog without much success. I managed to get folder paths using FolderBrowserDialog.SelectedPath. Using the OpenFileDialog class, I could not figure out how to get the file path.

Even if I could understand this, I'm still in a bunch. I have a user who selects a folder or file through some kind of dialog. I do not know if the path will belong to a folder or file. When the user clicks the "Open" button, I want the current selected directory or file path to be saved in a string. It seems that each of the classes that I used is stuck in files or folders.

Is this possible with WinForms dialogs? I would rather not write my own dialogue at this time.

+6
source share
2 answers
 openFileDialog1.ShowDialog(); string filePath = openFileDialog1.FileName; 

Gives you the path to the file.

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

for the folder.

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

to check what it is.

+7
source

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

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

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

You can simply copy FolderBrowserDialogEx.cs into your project.

+2
source

Source: https://habr.com/ru/post/914588/


All Articles