FileDialog 类

显示一个用户可从中选择文件的对话框窗口。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public MustInherit Class FileDialog
    Inherits CommonDialog
用法
Dim instance As FileDialog
public abstract class FileDialog : CommonDialog
public ref class FileDialog abstract : public CommonDialog
public abstract class FileDialog extends CommonDialog
public abstract class FileDialog extends CommonDialog

备注

FileDialog 为包含 OpenFileDialogSaveFileDialog 类的通用行为的抽象类。虽然它包含这两个类的通用行为,但不应直接使用。不能创建 FileDialog 的实例。尽管该类声明为公共的,但不能从它继承,因为它包含内部抽象方法。要创建对话框以选择或保存文件,请使用 OpenFileDialogSaveFileDialog

FileDialog 是有模式对话框,因此在显示时,它会阻止其他应用程序的运行,直到用户选定文件为止。当对话框有模式显示时,不能进行任何输入(通过键盘或鼠标单击),对对话框上的对象的输入除外。在对调用程序进行输入之前,程序必须隐藏或关闭对话框(通常是响应某一用户操作)。

警告

当使用从 FileDialog 派生的类(如 OpenFileDialogSaveFileDialog)时,应避免使用包含绝对路径的字符串。而应使用下表介绍的一种或多种技术动态地获取路径。

有多种可用于创建目录路径的方式,具体使用哪种方式取决于应用程序类型、与应用程序关联的数据的存储方式以及访问文件系统的原因。下表列出了用于动态创建路径的技术。

路径或程序类别

要使用的类和成员

标准 Windows 路径,如 Program Files、MyDocuments、Desktop 等

可以从 System.Environment 类非常完整地获取这些路径,例如通过其静态方法(如 SystemDirectory),或通过 GetFolderPath 方法(使用 Environment.SpecialFolder 枚举值之一)来获取。

与当前应用程序相关的路径

Application 类具有获取某些路径的静态成员,如 StartupPathExecutablePathLocalUserAppDataPathCommonAppDataPath

System.IO.PathGetTempPath 方法返回临时文件夹的路径。

System.IO.Directory 类的 GetCurrentDirectory 方法返回应用程序的当前执行路径。

DriveInfo 类的 RootDirectory 属性表示指定的驱动器的根目录。

作为应用程序设置存储的路径

访问从 ApplicationSettingsBase 派生的包装类的相应应用程序设置属性。有关更多信息,请参见 Windows 窗体的应用程序设置

注册表存储

某些应用程序在注册表中存储目录信息。Application 类具有解析为 RegistryKey 值的 CommonAppDataPathLocalUserAppDataPath 属性。

ClickOnce 应用程序

对于 ClickOnce 应用程序,请使用 Application 类成员(如 UserAppDataPath),它将返回指向 ClickOnce 数据目录的指针。有关更多信息,请参见 在 ClickOnce 应用程序中访问本地数据和远程数据

国际应用程序

对于国际应用程序,请通过使用 System.Resources.ResourceReader 类从应用程序的字符串资源中检索相对路径。有关全球化和本地化的更多信息,请参见主题 编码和本地化

注意,可能需要使用一个或多个所介绍的技术来生成完整路径。例如,GetFolderPath 方法可用于获取 MyDocuments 文件夹的路径,然后可使用应用程序设置来添加相对子目录部分。

System.IO.Path 类包含能辅助操作绝对和相对路径字符串的静态成员,而 System.IO.FileSystem.IO.Directory 类分别具有实际操作文件和目录的静态成员。

示例

下面的代码示例使用 FileDialogOpenFileDialog 实现,并演示如何创建和设置属性以及如何显示对话框。该示例使用 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.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.CommonDialog
        System.Windows.Forms.FileDialog
           System.Windows.Forms.OpenFileDialog
           System.Windows.Forms.SaveFileDialog

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

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