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. The following code example uses the OpenFileDialog implementation of FileDialog and illustrates creating, setting of properties, and showing the dialog box. The example uses the RestoreDirectory property to ensure that the previously selected directory is restored when the dialog box is closed. OpenFileDialog can be used to safely retrieve file names by applications running with partial trust (see Security (WPF)). Using Microsoft.Win32 removes the ability to use DialogResult and instead Nullable is used. For this question, it looks like it is the usage of System.Windows.Forms.OpenFileDialog, we can do like this. 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.

  1. How To: Read Text From A File
  2. OpenFileDialog.OpenFile Method (System.Windows.Forms ...
  3. System.windows.forms.openfiledialog Select Folder

How To: Read Text From A File

Note: This tip requires PowerShell 2.0 or above.

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.

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:

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

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

RadOpenFileDialog is a modal dialog box that allows you to specify one or multiple file names to open.

Figure 1: RadOpenFileDialog in single selection mode

Showing the Dialog

To show the dialog call its ShowDialog method. If a valid file is opened when you press OK, the DialogResult property will return OK and the FileName, and FileNames properties will be set. You can use FileName and FileNames to get the names of the selected items.

Note that when the ShowDialog method is called the UI of the host application will freeze until the dialog closes.

Example 1: Show an open file dialog

Opening the Selected File

You can open a read-only file stream for the selected file using the OpenFile method. Or alternatively you can use the FileName and FileNames properties and open the file manually.

Example 2: Open a file stream

Enabling Multiple Selection

OpenFileDialog.OpenFile Method (System.Windows.Forms ...

The dialog supports single and multiple selection modes. By default, you can select only one file at a time. To alter this you can set the Multiselect property of RadOpenFileDialog to True.

Example 3: Enable multiple selection

Figure 2: Multiple selection

Working with the Selected Files

You can get the paths of the selected files via the FileName and FileNames properties. Note that the properties are empty until the DialogResult is valid. When you open file(s) the properties will return the corresponding paths.

You can get only the name of the selected files, without the full path, via the SafeFileNames collection property.

Windows Forms Openfiledialog

Example 4: Get the selected file names

The FileName property can be set manually. This will change the value displayed in the selected file auto-complete box area. Note that setting this won't change the selected item in the list with the files.

System.windows.forms.openfiledialog Select Folder

Saving the Last Used Directory

Windows Forms Openfiledialog

You can save the last used directory by setting the RestoreDirectory property of the RadOpenFileDialog. After setting this property to True and opening a file the InitialDirectory of this RadOpenFileDialog instance will be set to the parent folder of the opened file.

Example 5: Set RestoreDirectory property

Enabling ReadOnly CheckBox

Forms

You can display a checkbox to control whether the file should be opened in readonly mode with the ShowReadOnly property of the RadOpenFileDialog. You can control the state of that checkbox by using the ReadOnlyChecked property of the RadOpenFileDialog.

Example 6: Enabling the ReadOnly CheckBox

Windows forms openfiledialog filter

Figure 3: RadOpenFileDialog with Checked ReadOnly CheckBox

DereferenceLinks

RadOpenFileDialog exposes a DereferenceLinks property indicating whether a file dialog returns the location of the file referenced by a shortcut or the location of the actual shortcut file (with the .lnk extension).

Example 7: Using the DereferenceLinks property

If in multiple or single selection the first selected item is a link to a directory and DereferenceLinks is set to True, clicking the Open button will actually navigate to this directory.