# OpenFileDialog

This class allows you to check whether a file exists and to open it. The `ShowReadOnly` property determines whether a read-only check box appears in the dialog box. The `ReadOnlyChecked` property indicates whether the read-only check box is checked.

Most of the core functionality for this class is found in the `FileDialog` class.

If you want to give the user the ability to select a folder instead of a file, use [FolderBrowserDialog](https://docs.wisej.com/docs/controls/common-dialogs/folderbrowserdialog) instead.

{% hint style="info" %}
For a full list of properties, methods and events see the [API documentation.](http://docs.wisej.com/api)
{% endhint %}

## Features

### Filtering

The OpenFileDialog filters files shown to the user based on a given pattern. For example, the filter `C# files|*.cs|All files|*.*` will show files ending with .cs by default. Users can modify the filter using the dropdown menu.

![OpenFileDialog showing file type filtering options](https://553579532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MF1D11gPs_az3xaKusw%2Fuploads%2Fgit-blob-9cfdccdfc3fbaac16f8cff45069ab00bf155e713%2Fimage.png?alt=media)

{% hint style="success" %}
The [FileDialog.FileName](https://docs.wisej.com/api/wisej.web/common-dialogs/wisej.web.filedialog#filename) property returns the virtual path relative to the root. To get the physical path on the server, use [FileDialog.MapPath()](https://docs.wisej.com/api/wisej.web/common-dialogs/wisej.web.filedialog#mappath-filepath).
{% endhint %}

### Help

The OpenFileDialog includes an optional "?" (Help) tool for displaying navigation assistance.

![OpenFileDialog showing help button and navigation interface](https://553579532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MF1D11gPs_az3xaKusw%2Fuploads%2Fgit-blob-237c3ba431b354f1dc57e641d2d5bfca27fd4b98%2Fimage.png?alt=media)

## How To

### Connect to the File System

Add at least one `IFileSystemProvider` to the Roots collection before showing the dialog:

```csharp
//Using an absolute file path
OpenFileDialog filedialog = new OpenFileDialog(); 
filedialog.Roots.Add(new FileSystemProvider("C:\\", "Main_Directory"));
```

```csharp
//Using a relative file path
OpenFileDialog filedialog = new OpenFileDialog(); 
filedialog.Roots.Add(new FileSystemProvider("./", "Main_Directory"));
```

### Customize the Appearance

The OpenFileDialog supports customization of individual controls including textboxes and pickers.

To customize these controls:

1. Create a class inheriting from `FileDialogUI`
2. Add the custom `OpenFileDialog` constructor
3. Modify the controls as needed

{% tabs %}
{% tab title="C#" %}

```csharp
public class CustomOpenFileDialogUI : FileDialogUI
{
    public CustomOpenFileDialogUI(OpenFileDialog openFileDialog) : base(openFileDialog)
    {
        this.HeaderBackColor = Color.DarkOrange;
        foreach (ColumnHeader col in this.listView.Columns)
        {
            col.BackColor = Color.DarkOrange;
        }
    }
}
```

{% endtab %}
{% endtabs %}

After applying the OpenFileDialog's `DialogTemplate` property to the new class, the result looks like this:

![Customized OpenFileDialog with dark orange theme applied](https://553579532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MF1D11gPs_az3xaKusw%2Fuploads%2Fgit-blob-6f4ec7d5412cf00c1d86ffdc737c94e5322d0cd5%2Fimage.png?alt=media)

For advanced customization of the `OpenFileDialog` using custom templates, see below.

### Create a Custom UI by deriving from FileDialogUI

Create a custom class that derives from FileDialogUI:

```csharp
public class CustomFileBrowserUI : FileDialogUI
{
    public CustomFileBrowserUI(): base() 
    {
        InitializeComponent();
    }
}
```

You can then create a new OpenFileDialog and set the DialogTemplate like so. Note that you can set properties here such as the InitialDirectory and MultiSelect. These properties must be set before opening the file dialog with ShowDialog.

```csharp
var openFileDialog = new OpenFileDialog {
    DialogTemplate = typeof(CustomFileBrowserUI),
    InitialDirectory = "...",
   Multiselect = true
};
openFileDialog.ShowDialog();
```

You should always dispose the FileDialog after use. An easy way to do so is to place it in a `using` statement like so:

```csharp
using (var openFileDialog = new OpenFileDialog {
    DialogTemplate = typeof(CustomFileBrowserUI),
    InitialDirectory = "...",
   Multiselect = true
}) {
   openFileDialog.ShowDialog();
}
```
