Windows Forms Openfiledialog

Windows Forms Openfiledialog 4,1/5 1988 votes
Active1 year, 7 months ago

I'm writing a little utility that starts with selecting a file, and then I need to select a folder. I'd like to default the folder to where the selected file was.

OpenFileDialog.FileName returns the full path & filename - what I want is to obtain just the path portion (sans filename), so I can use that as the initial selected folder.

Are there any .NET methods to do this, or do I need to use regex, split, trim, etc??

NightOwl888

Feb 11, 2019  The System.Windows.Forms.OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog.OpenFile method, or create an instance of the System.IO.StreamReader class. The following examples show both approaches. How to: Open files with the OpenFileDialog.; 4 minutes to read +8; In this article. The System.Windows.Forms.OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog.OpenFile method, or create an instance of the System.IO.StreamReader.

44.2k20 gold badges107 silver badges174 bronze badges
Kevin Haines

Windows Forms Controls

Kevin Haines
1,3873 gold badges11 silver badges19 bronze badges

5 Answers

Use the Path class from System.IO. It contains useful calls for manipulating file paths, including GetDirectoryName which does what you want, returning the directory portion of the file path.

Usage is simple.

Jeff YatesJeff Yates
54.2k18 gold badges133 silver badges179 bronze badges
Jan MacháčekJan Macháček
Adi Lester
20.6k12 gold badges78 silver badges98 bronze badges
MaxMax

You can use FolderBrowserDialog instead of FileDialog and get the path from the OK result.

Mafii
5,2801 gold badge25 silver badges46 bronze badges
ShaahinShaahin
5751 gold badge9 silver badges19 bronze badges
AbdelAbdel

Not the answer you're looking for? Browse other questions tagged c#.netparsingpath or ask your own question.

Active2 years, 2 months ago

How can I get the result (meaning the file name and its location) from an Open File Dialog?

My code:

Omar
13k7 gold badges36 silver badges60 bronze badges
user1678541

Windows Forms Filedialog

5 Answers

sllsll

System Windows Forms Openfiledialog

51.2k15 gold badges89 silver badges137 bronze badges

The OpenFileDialog class has a FileName property for that.

Typically, you want to make sure the user didn't cancel the dialog:

LarsTechOpenfiledialogLarsTech
72.3k13 gold badges121 silver badges176 bronze badges

This Link may be useful: http://softsprogrammer.blogspot.in/2014/03/openfiledialog-in-c.html

System.windows.forms.openfiledialog Multiselect Powershell

The page shows hot to use OpenFileDialog in a Windows Form.

These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. Fpse free download. We also use third-party cookies that help us analyze and understand how you use this website.

Tijo TomTijo Tom
Oded

C# Open Dialog

Oded
426k78 gold badges784 silver badges933 bronze badges
Aghilas YakoubAghilas Yakoub

C# File Dialog Box

25.3k4 gold badges33 silver badges39 bronze badges