6

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.

Cheese
  • 1,429
  • 3
  • 16
  • 17
  • Have you looked at all at the System.IO.Path namespace? There is lots of good stuff in there. – WildCrustacean Apr 30 '12 at 23:10
  • http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.attributes.aspx will tell you if it is a directory – Patrick Apr 30 '12 at 23:46
  • I updated the main post to clarify a couple things. bde - The problem is that I don't know the path until the user specifies it through a dialog. Yorye - Thanks. Even though I said I wasn't sure how to get the file name, it's not quite what I'm after. Patrick - I won't know the path name until the user chooses it with a dialog, so there's nothing to really check. – Cheese May 01 '12 at 17:37

1 Answers1

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

ispiro
  • 26,556
  • 38
  • 136
  • 291