OpenFileDialog Class


显示一个标准对话框,提示用户打开文件。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.ShowDialog 方法显示对话框。The 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.

此类的大部分核心功能位于 FileDialog 类中。Most 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.

(Inherited from 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.

(Inherited from FileDialog)

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

(Inherited from 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.

(Inherited from FileDialog)
ClientGuid (Inherited from FileDialog)

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

(Inherited from Component)

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

(Inherited from FileDialog)

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

(Inherited from 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).

(Inherited from FileDialog)

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

(Inherited from Component)

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

(Inherited from Component)

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

(Inherited from FileDialog)

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

(Inherited from 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.

(Inherited from FileDialog)

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

(Inherited from FileDialog)

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

(Inherited from FileDialog)

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

(Inherited from FileDialog)

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


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

(Inherited from 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.

(Inherited from 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.

(Inherited from 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.

(Inherited from Component)

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

(Inherited from FileDialog)

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

(Inherited from CommonDialog)

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

(Inherited from FileDialog)

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

(Inherited from FileDialog)



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

(Inherited from MarshalByRefObject)

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

(Inherited from Component)

释放 Component 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(Inherited from Component)

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

(Inherited from Object)

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

(Inherited from Object)

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

(Inherited from MarshalByRefObject)

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

(Inherited from Component)

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

(Inherited from 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.

(Inherited from FileDialog)

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

(Inherited from MarshalByRefObject)

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

(Inherited from Object)

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

(Inherited from MarshalByRefObject)

引发 FileOk 事件。Raises the FileOk event.

(Inherited from FileDialog)

引发 HelpRequest 事件。Raises the HelpRequest event.

(Inherited from 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.

(Inherited from CommonDialog)

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


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

(Inherited from FileDialog)

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

(Inherited from CommonDialog)

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

(Inherited from CommonDialog)

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

(Inherited from FileDialog)



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

(Inherited from Component)

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

(Inherited from FileDialog)

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

(Inherited from CommonDialog)

Applies to

See also