Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a folder or file path through a single WinForm dialog? [duplicate]

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.

like image 736
Cheese Avatar asked Apr 30 '12 23:04

Cheese


People also ask

How do I find the path of a specific file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I move a file from one directory to another in C#?

Syntax. public static void Move (string sourceDirName, string destDirName); This method takes the source directory/file path and the destination directory/file path as input. It creates a new directory with the destination directory name and moves the source directory (or file) to the destination.

What is the use of FolderBrowserDialog in C#?

A FolderBrowserDialog control is used to browse and select a folder on a computer. A typical FolderBrowserDialog looks like Figure 1 where you can see Windows Explorer-like features to navigate through folders and select a folder.


1 Answers

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.

like image 113
ispiro Avatar answered Oct 03 '22 04:10

ispiro