FileDialog クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


FileDialog は、OpenFileDialog クラスと SaveFileDialog クラスの共通する動作を定義する抽象クラスです。このクラスは、直接使用するためのものではなく、これら 2 つのクラスの共通する動作を定義しています。FileDialog のインスタンスは作成できません。クラスはパブリックとして宣言されますが、内部抽象メソッドが定義されているため、このクラスからは継承できません。ファイルの選択または保存を行うためのダイアログ ボックスを作成するには、OpenFileDialog または SaveFileDialog を使用します。
FileDialog はモーダル ダイアログ ボックスです。このため、このダイアログ ボックスが表示されているときは、ユーザーがファイルを選択するまでアプリケーションの他の機能は動作しません。ダイアログ ボックスがモーダルとして表示されている場合、キーボードやマウス クリックによる入力は、ダイアログ ボックスのオブジェクトに対してしか発生しません。呼び出しプログラムに対する入力が発生する前に、プログラムが (通常、何らかのユーザーの操作に対する応答として) ダイアログ ボックスを非表示にするか、または終了する必要があります。
![]() |
---|
FileDialog の派生クラス (OpenFileDialog や SaveFileDialog など) を使用するときは、絶対パスを含むリテラル文字列を使用しないようにします。代わりに、次の表に記載された手法のいずれかを使用して、動的にパスを取得します。 |
アプリケーションの種類、アプリケーションに関連付けられたデータの格納方法、およびファイル システムにアクセスする理由に応じて、ディレクトリ パスを作成する方法は多数あります。パスを動的に作成する手法を次の表に示します。
完全パスを作成するために、上記のうち 1 つ以上の手法が必要となる場合があります。たとえば、GetFolderPath メソッドを使用して My Documents フォルダへのパスを取得してから、アプリケーション設定を使用して相対サブディレクトリ部分を追加する場合があります。
System.IO.Path クラスには、絶対パス文字列および相対パス文字列の処理を支援する静的メンバが用意されているのに対し、System.IO.File クラスと System.IO.Directory クラスには、実際にファイルとディレクトリを直接処理する静的メンバが、それぞれ用意されています。

FileDialog の OpenFileDialog 実装を使用し、ダイアログ ボックスの作成、そのプロパティの設定、およびダイアログ ボックスの表示を実行するコード例を次に示します。この例では、ShowDialog メソッドを使用してダイアログ ボックスを表示し、DialogResult を返しています。この例では、フォームに Button が配置されており、System.IO 名前空間が追加されている必要があります。
Private Sub button1_Click(sender As Object, e As System.EventArgs) Dim myStream As Stream Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = DialogResult.OK Then myStream = openFileDialog1.OpenFile() If Not (myStream Is Nothing) Then ' Insert code to read the stream here. myStream.Close() End If End If End Sub
private void button1_Click(object sender, System.EventArgs e) { Stream myStream; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { if((myStream = openFileDialog1.OpenFile())!= null) { // Insert code to read the stream here. myStream.Close(); } } }
private: void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { Stream^ myStream; OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; openFileDialog1->InitialDirectory = "c:\\"; openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1->FilterIndex = 2; openFileDialog1->RestoreDirectory = true; if ( openFileDialog1->ShowDialog() == ::DialogResult::OK ) { if ( (myStream = openFileDialog1->OpenFile()) != nullptr ) { // Insert code to read the stream here. myStream->Close(); } } }
protected void button1_Click(Object sender, System.EventArgs e) { Stream myStream; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.set_InitialDirectory("c:\\"); openFileDialog1.set_Filter( "txt files (*.txt)|*.txt|All files (*.*)|*.*"); openFileDialog1.set_FilterIndex(2); openFileDialog1.set_RestoreDirectory(true); if (openFileDialog1.ShowDialog().Equals(get_DialogResult().OK)) { if ((myStream = openFileDialog1.OpenFile()) != null) { // Insert code to read the stream here. myStream.Close(); } } } //button1_Click

System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.CommonDialog
System.Windows.Forms.FileDialog
System.Windows.Forms.OpenFileDialog
System.Windows.Forms.SaveFileDialog


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


FileDialog メンバ
System.Windows.Forms 名前空間
CommonDialog クラス
OpenFileDialog
SaveFileDialog
System.IO.Path
System.IO.File
System.IO.Directory
System.Environment
Application クラス
Microsoft.Win32.Registry
System.Resources.ResourceReader
- FileDialog クラスのページへのリンク