コモン ダイアログ ボックスを開く方法 (WPF .NET)

この記事では、Windows Foundation Presentation (WPF) でコモン システム ダイアログ ボックスを表示する方法について説明します。 Windows では、ファイルを選択して印刷するためのダイアログ ボックスなど、すべてのアプリケーションに共通となるさまざまな再利用可能ダイアログ ボックスが実装されます。

そのようなダイアログ ボックスはオペレーティング システムから提供されるため、オペレーティング システム上で実行されるあらゆるアプリケーションの間で共有されます。 これらのダイアログ ボックスは使い方が一貫して同じであり、"コモン ダイアログ ボックス" と呼ばれています。 あるアプリケーションであるコモン ダイアログ ボックスを使用しているなら、他のアプリケーションでそのダイアログ ボックスの使い方を覚える必要がありません。

メッセージ ボックスもコモン ダイアログ ボックスの一例です。 詳細については、メッセージ ボックスを開く方法に関するページを参照してください。

[ファイルを開く] ダイアログ ボックス

[ファイルを開く] ダイアログ ボックスは、開くファイルの名前を取得するために、ファイルを開く機能によって使用されます。

An Open dialog box showing the location to retrieve the file shown from a WPF application.

[ファイルを開く] コモン ダイアログ ボックスは、OpenFileDialog クラスとして実装され、Microsoft.Win32 名前空間にあります。 次のコードは、ダイアログを作成、構成、表示する方法を示しています。

// Configure open file dialog box
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
bool? result = dialog.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dialog.FileName;
}
' Configure open file dialog box
Dim dialog As New Microsoft.Win32.OpenFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show open file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open file dialog box results
If result = True Then
    ' Open document
    Dim filename As String = dialog.FileName
End If

ファイルを開くダイアログ ボックスの詳細については、「Microsoft.Win32.OpenFileDialog」を参照してください。

[ファイルの保存] ダイアログ ボックス

[ファイルの保存] ダイアログ ボックスは、保存するファイルの名前を取得するために、ファイルを保存する機能によって使用されます。

A Save As dialog box showing the location to save the file shown from a WPF application.

[ファイルの保存] コモン ダイアログ ボックスは、SaveFileDialog クラスとして実装され、Microsoft.Win32 名前空間にあります。 次のコードは、ダイアログを作成、構成、表示する方法を示しています。

// Configure save file dialog box
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dialog.FileName;
}
' Configure save file dialog box
Dim dialog As New Microsoft.Win32.SaveFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Save document
    Dim filename As String = dialog.FileName
End If

[ファイルの保存] ダイアログ ボックスの詳細については、「Microsoft.Win32.SaveFileDialog」を参照してください。

[フォルダーを開く] ダイアログ ボックス

重要

[フォルダーを開く] ダイアログ ボックスは、.NET 8.0 以降で使用できます。

[フォルダーを開く] ダイアログ ボックスは、ユーザーが 1 つ以上のフォルダーを選択し、プログラムに戻すために使用します。 たとえば、ファイルの量やフォルダー内のファイル名など、フォルダーに関する情報がプログラムに表示された場合、ユーザーは [フォルダーを開く] ダイアログを使用してフォルダーを選択できます。

An Open Folder dialog box showing the Pictures folder with the Camera Roll folder selected, shown from a WPF application.

[フォルダーを開く] コモン ダイアログ ボックスは、OpenFolderDialog クラスとして実装され、Microsoft.Win32 名前空間にあります。 次のコードは、ダイアログを作成、構成、表示する方法を示しています。

// Configure open folder dialog box
Microsoft.Win32.OpenFolderDialog dialog = new();

dialog.Multiselect = false;
dialog.Title = "Select a folder";

// Show open folder dialog box
bool? result = dialog.ShowDialog();

// Process open folder dialog box results
if (result == true)
{
    // Get the selected folder
    string fullPathToFolder = dialog.FolderName;
    string folderNameOnly = dialog.SafeFolderName;
}
' Configure open folder dialog box
Dim dialog As New Microsoft.Win32.OpenFolderDialog()

dialog.Multiselect = True
dialog.Title = "Select a folder"

' Show open folder dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open folder dialog box results
If result = True Then

    ' Get multiple folder names
    For index = 0 To dialog.FolderNames.Length
        ' Get the selected folder
        Dim fullPathToFolder As String = dialog.FolderNames(index)
        Dim folderNameOnly As String = dialog.SafeFolderNames(index)
    Next

End If

[フォルダーを開く] ダイアログ ボックスの詳細については、「Microsoft.Win32.OpenFolderDialog」を参照してください。

[印刷] ダイアログ ボックスは、ユーザーがデータを印刷するプリンターを選択し、構成するために、印刷機能によって使用されます。

A print dialog box shown from a WPF application.

[印刷] コモン ダイアログ ボックスは、PrintDialog クラスとして実装され、System.Windows.Controls 名前空間にあります。 次のコードは、[印刷] ダイアログ ボックスの作成、構成、および表示の方法を示しています。

// Configure printer dialog box
var dialog = new System.Windows.Controls.PrintDialog();
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages;
dialog.UserPageRangeEnabled = true;

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Document was printed
}
' Configure printer dialog box
Dim dialog As New System.Windows.Controls.PrintDialog()
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages
dialog.UserPageRangeEnabled = True

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Document was printed
End If

[印刷] ダイアログ ボックスの詳細については、「System.Windows.Controls.PrintDialog」を参照してください。 WPF での印刷の詳細については、「印刷の概要」を参照してください。

関連項目