Windows Forms Openfiledialog

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. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. The application must also be compiled with debugging enabled. For example: windows.forms jitDebugging='true' / When JIT debugging is enabled, any unhandled exception will be sent to the JIT debugger registered on the computer rather than be handled by this dialog box.

I wanted to create a form element that looks like this.

Forms

The solution was such that when users click 'Browse', a open file dialog will show up to allow them to choose a file for my program to process. In this post, I shall discuss how I had implemented this solution.

Elements on the windows form

The classes needed for this solution are as follows:

Except for OpenFileDialog, all of the above components will have their visuals on the windows form:

Implementing the logic to show the file dialog when users click 'Browse'

#PSTip Using the System.Windows.Forms.OpenFileDialog Class ...

Double-click on the 'Browse' button in the Designer to generate a function and associate it with the Button.Click event. In the generated code, include the codes to show the OpenFileDialog:

With that, we are able to get the file URL that users have selected from the text box. Alternatively, if we are just going to process a file, we could have just call our backend code from within the if statement. However, for my case, I will want to get more user input before I process the file, that was why I had set the file URL to the text box. This will allow my program to get the file URL of the selected file when users had submitted more input.

Other configurations

Set the TextBox as readonly

Similar to a file upload html element, I will only allow my users to see which file that they have selected. By setting the ReadOnly property of the TextBox instance to true, users can click on the file URL text to verify their selection but not able to change the contents with their keyboards.

Set the file filter for the OpenFileDialog

By default, the OpenFileDialog allows users to select any files that they want. However, in my application, I only want them to select .txt files. By setting the Filter property of the OpenFileDialog instance, I can configure the OpenFileDialog to only display the type of files I want my users to select. The easiest way is to do so in the designer:

Forms

About Clivant

Windows Forms Openfiledialog

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

← Previous post
Handling web server communication feedback with System.Net.WebException in C#
Next post →
Uploading large HTTP multipart request with System.Net.HttpWebRequest in C#

Note: This tip requires PowerShell 2.0 or above.

C# OpenFileDialog Example - Dot Net Perls

In PowerShell, it is possible to use GUI elements to request user input. Although it is possible to create your own forms from scratch, there are also many useful pre-built dialogs available. In this tip, I will show you how to use the System.Windows.Forms.OpenFileDialog to select one or multiple files.

Forms

The following code will open a window that will prompt the user to select a single file. By setting the InitialDirectory property, the starting directory will be set to the current user’s desktop. This is done by using the [Environment] Desktop special folder:

If documents need to be selected, it can be useful to set the starting folder to the documents folder. By setting a filter, we can ensure that only a certain type of file is selected. The next code sample will allow users to select .docx files. The filter can be changed by the user to also select an xlsx file:

Windows Forms Openfiledialog Example

To select multiple files the MultiSelect property should be set to True.

How To: Open Files With The OpenFileDialog Component ...

For more information about this class the following MSDN article can be used: