OpenFileDialog 类


显示一个标准对话框,提示用户打开文件。Displays a standard dialog box that prompts the user to open a file. 此类不能被继承。This class cannot be inherited.

public ref class OpenFileDialog sealed : System::Windows::Forms::FileDialog
public sealed class OpenFileDialog : System.Windows.Forms.FileDialog
type OpenFileDialog = class
    inherit FileDialog
Public NotInheritable Class OpenFileDialog
Inherits FileDialog


下面的代码示例创建一个 OpenFileDialog ,设置多个属性以定义文件扩展名筛选器和对话框行为,并使用方法显示对话框 CommonDialog.ShowDialogThe following code example creates an OpenFileDialog, sets several properties to define the file extension filter and dialog behavior, and displays the dialog box using the CommonDialog.ShowDialog method. 该示例要求在窗体 Button 上放置了一个并对 System.IO 其添加了命名空间的引用。The example requires a form with a Button placed on it and a reference to the System.IO namespace added to it.

   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() == System::Windows::Forms::DialogResult::OK )
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
            // Insert code to read the stream here.
var fileContent = string.Empty;
var filePath = string.Empty;

using (OpenFileDialog openFileDialog = new OpenFileDialog())
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
        //Get the path of specified file
        filePath = openFileDialog.FileName;

        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
            fileContent = reader.ReadToEnd();

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing
    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() = System.Windows.Forms.DialogResult.OK Then
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Insert code to read the stream here.
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
            End If
        End Try
    End If
End Sub


此类可用于检查文件是否存在并打开它。This class allows you to check whether a file exists and to open it. ShowReadOnly属性确定是否在对话框中出现只读复选框。The ShowReadOnly property determines whether a read-only check box appears in the dialog box. ReadOnlyChecked属性指示是否选中只读复选框。The ReadOnlyChecked property indicates whether the read-only check box is checked.

此类的大部分核心功能都可在类中找到 FileDialogMost of the core functionality for this class is found in the FileDialog class.

在从右到左的操作系统上,设置包含窗体的 RightToLeft 属性以设置 RightToLeft.Yes 对话框的 文件名打开取消 按钮的外观。On a right-to-left operating system, setting the containing form's RightToLeft property to RightToLeft.Yes localizes the dialog's File Name, Open, and Cancel buttons. 如果该属性未设置为 RightToLeft.Yes ,则改为使用英语文本。If the property is not set to RightToLeft.Yes, English text is used instead.

如果要使用户能够选择文件夹而不是文件,请改用 FolderBrowserDialogIf you want to give the user the ability to select a folder instead of a file, use FolderBrowserDialog instead.



初始化 OpenFileDialog 类的实例。Initializes an instance of the OpenFileDialog class.



获取或设置一个值,该值指示如果用户省略扩展名,对话框是否自动在文件名中添加扩展名。Gets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.

(继承自 FileDialog)

获取或设置一个值,该值指示此 FileDialog 实例在 Windows VistaWindows Vista 上运行时是否应自动升级外观和行为。Gets or sets a value indicating whether this FileDialog instance should automatically upgrade appearance and behavior when running on Windows VistaWindows Vista.

(继承自 FileDialog)

获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(继承自 Component)

获取或设置一个值,该值指示如果用户指定不存在的文件名,对话框是否显示警告。Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.


获取或设置一个值,该值指示如果用户指定不存在的路径,对话框是否显示警告。Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a path that does not exist.

(继承自 FileDialog)

获取或设置与此对话框状态关联的 GUID。Gets or sets the GUID to associate with this dialog state. 通常情况下,状态(如最后访问的文件夹)和对话框的位置及大小将根据可执行文件的名称持久保存。Typically, state such as the last visited folder and the position and size of the dialog is persisted based on the name of the executable file. 通过指定 GUID,一个应用程序对于同一应用程序中不同版本的对话框(例如,导入的对话框和打开的对话框),可以具有不同的持久状态。By specifying a GUID, an application can have different persisted states for different versions of the dialog within the same application (for example, an import dialog and an open dialog).

如果应用程序未使用视觉样式或如果 AutoUpgradeEnabled 设置为 false,则此功能不可用。This functionality is not available if an application is not using visual styles or if AutoUpgradeEnabled is set to false.

(继承自 FileDialog)

获取包含 IContainerComponentGets the IContainer that contains the Component.

(继承自 Component)

获取此 FileDialog 实例的自定义空间的集合。Gets the custom places collection for this FileDialog instance.

(继承自 FileDialog)

获取或设置默认文件扩展名。Gets or sets the default file name extension.

(继承自 FileDialog)

获取或设置一个值,该值指示对话框是否返回快捷方式引用的文件的位置,或者是否返回快捷方式 (.lnk) 的位置。Gets or sets a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk).

(继承自 FileDialog)

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(继承自 Component)

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(继承自 Component)

获取或设置一个包含在文件对话框中选定的文件名的字符串。Gets or sets a string containing the file name selected in the file dialog box.

(继承自 FileDialog)

获取对话框中所有选定文件的文件名。Gets the file names of all selected files in the dialog box.

(继承自 FileDialog)

获取或设置当前文件名筛选器字符串,该字符串决定对话框的“另存为文件类型”或“文件类型”框中出现的选择内容。Gets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.

(继承自 FileDialog)

获取或设置文件对话框中当前选定筛选器的索引。Gets or sets the index of the filter currently selected in the file dialog box.

(继承自 FileDialog)

获取或设置文件对话框显示的初始目录。Gets or sets the initial directory displayed by the file dialog box.

(继承自 FileDialog)

获取应用程序的 Win32 实例句柄。Gets the Win32 instance handle for the application.

(继承自 FileDialog)

获取或设置一个值,该值指示对话框是否允许选择多个文件。Gets or sets a value indicating whether the dialog box allows multiple files to be selected.


获取用来初始化 FileDialog 的值。Gets values to initialize the FileDialog.

(继承自 FileDialog)

获取或设置一个值,该值指示是否选定只读复选框。Gets or sets a value indicating whether the read-only check box is selected.


获取或设置一个值,该值指示该对话框在关闭前是否将目录还原为之前选定的目录。Gets or sets a value indicating whether the dialog box restores the directory to the previously selected directory before closing.

(继承自 FileDialog)

获取对话框中所选文件的文件名和扩展名。Gets the file name and extension for the file selected in the dialog box. 文件名不包含路径。The file name does not include the path.


获取对话框中所有选定文件的文件名和扩展名的数组。Gets an array of file names and extensions for all the selected files in the dialog box. 文件名不包含路径。The file names do not include the path.


获取或设置一个值,该值指示文件对话框中是否显示“帮助”按钮****。Gets or sets a value indicating whether the Help button is displayed in the file dialog box.

(继承自 FileDialog)

获取或设置一个值,该值指示对话框是否包含只读复选框。Gets or sets a value indicating whether the dialog box contains a read-only check box.


获取或设置 ComponentISiteGets or sets the ISite of the Component.

(继承自 Component)

获取或设置对话框是否支持显示和保存具有多个文件扩展名的文件。Gets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.

(继承自 FileDialog)

获取或设置一个对象,该对象包含控件的数据。Gets or sets an object that contains data about the control.

(继承自 CommonDialog)

获取或设置文件对话框标题。Gets or sets the file dialog box title.

(继承自 FileDialog)

获取或设置一个值,该值指示对话框是否只接受有效的 Win32 文件名。Gets or sets a value indicating whether the dialog box accepts only valid Win32 file names.

(继承自 FileDialog)



创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)

释放由 Component 使用的所有资源。Releases all resources used by the Component.

(继承自 Component)

释放由 Component 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(继承自 Component)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(继承自 Component)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
HookProc(IntPtr, Int32, IntPtr, IntPtr)

定义要重写的通用对话框挂钩过程,以便向文件对话框添加特定功能。Defines the common dialog box hook procedure that is overridden to add specific functionality to the file dialog box.

(继承自 FileDialog)

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)

引发 FileOk 事件。Raises the FileOk event.

(继承自 FileDialog)

引发 HelpRequest 事件。Raises the HelpRequest event.

(继承自 CommonDialog)

打开用户选定的具有只读权限的文件。Opens the file selected by the user, with read-only permission. 该文件由 FileName 属性指定。The file is specified by the FileName property.

OwnerWndProc(IntPtr, Int32, IntPtr, IntPtr)

定义要重写的所有者窗口过程,以便向通用对话框添加特定功能。Defines the owner window procedure that is overridden to add specific functionality to a common dialog box.

(继承自 CommonDialog)

将所有属性重新设置为其默认值。Resets all properties to their default values.


指定通用对话框。Specifies a common dialog box.

(继承自 FileDialog)

用默认的所有者运行通用对话框。Runs a common dialog box with a default owner.

(继承自 CommonDialog)

运行具有指定所有者的通用对话框。Runs a common dialog box with the specified owner.

(继承自 CommonDialog)

提供此对象的字符串版本。Provides a string version of this object.

(继承自 FileDialog)



在通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(继承自 Component)

当用户单击文件对话框中的“打开”或 “保存”按钮时发生********。Occurs when the user clicks on the Open or Save button on a file dialog box.

(继承自 FileDialog)

当用户单击通用对话框中的“帮助”按钮时发生。Occurs when the user clicks the Help button on a common dialog box.

(继承自 CommonDialog)