通过


OpenFileDialog 类

定义

显示一个标准对话框,提示用户打开文件。 无法继承此类。

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 。 该示例需要一个窗体,其中包含一个 Button 放置在它的窗体以及对添加到它的命名空间的 System.IO 引用。

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() == System::Windows::Forms::DialogResult::OK )
      {
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
         {
            // Insert code to read the stream here.
            myStream->Close();
         }
      }
   }
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
        Try
            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)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End Try
    End If
End Sub

注解

此类允许你检查文件是否存在并打开它。 该 ShowReadOnly 属性确定对话框中是否显示只读复选框。 该 ReadOnlyChecked 属性指示是否选中只读复选框。

此类的大部分核心功能都位于该类中 FileDialog

在从右到左的操作系统上,将包含窗体 RightToLeft 的属性设置为 RightToLeft.Yes 本地化对话框的 “文件名”、“ 打开”和 “取消 ”按钮。 如果未将属性设置为 RightToLeft.Yes,则改用英语文本。

如果要让用户能够选择文件夹而不是文件,请改用 FolderBrowserDialog

构造函数

名称 说明
OpenFileDialog()

初始化类的 OpenFileDialog 实例。

属性

名称 说明
AddExtension

获取或设置一个值,该值指示如果用户省略扩展名,对话框是否自动向文件名添加扩展名。

(继承自 FileDialog)
AddToRecent

获取或设置一个值,该值指示对话框是将打开的文件添加还是保存到最近的列表中。

(继承自 FileDialog)
AutoUpgradeEnabled

获取或设置一个值,该值指示此 FileDialog 实例是否应在 Windows Vista 上运行时自动升级外观和行为。

(继承自 FileDialog)
CanRaiseEvents

获取一个值,该值指示组件是否可以引发事件。

(继承自 Component)
CheckFileExists

获取或设置一个值,该值指示如果用户指定不存在的文件名,对话框是否显示警告。

CheckPathExists

获取或设置一个值,该值指示如果用户指定不存在的路径,对话框是否显示警告。

(继承自 FileDialog)
ClientGuid

获取或设置要与此对话框状态关联的 GUID。 通常,状态(如上次访问的文件夹)和对话框的位置和大小会根据可执行文件的名称持久保存。 通过指定 GUID,应用程序可以在同一应用程序中为不同版本的对话具有不同的持久状态(例如,导入对话框和打开的对话)。

如果应用程序未使用视觉样式或 设置为 ,则此功能不可用。

(继承自 FileDialog)
Container

IContainer获取包含 .Component

(继承自 Component)
CustomPlaces

获取此 FileDialog 实例的自定义 places 集合。

(继承自 FileDialog)
DefaultExt

获取或设置默认文件扩展名。

(继承自 FileDialog)
DereferenceLinks

获取或设置一个值,该值指示对话框是否返回快捷方式引用的文件的位置,还是返回快捷方式的位置(.lnk)。

(继承自 FileDialog)
DesignMode

获取一个值,该值指示当前是否 Component 处于设计模式。

(继承自 Component)
Events

获取附加到此 Component对象的事件处理程序的列表。

(继承自 Component)
FileName

获取或设置一个字符串,其中包含在文件对话框中选择的文件名。

(继承自 FileDialog)
FileNames

获取对话框中所有选定文件的文件名。

(继承自 FileDialog)
Filter

获取或设置当前文件名筛选器字符串,该字符串确定对话框中的“另存为文件类型”或“类型文件”框中显示的选项。

(继承自 FileDialog)
FilterIndex

获取或设置文件对话框中当前选定的筛选器的索引。

(继承自 FileDialog)
InitialDirectory

获取或设置文件对话框显示的初始目录。

(继承自 FileDialog)
Instance

获取应用程序的 Win32 实例句柄。

(继承自 FileDialog)
Multiselect

获取或设置一个值,该值指示对话框是否允许选择多个文件。

OkRequiresInteraction

获取或设置一个值,该值指示是否禁用对话框的“确定”按钮,直到用户导航视图或编辑文件名(如果适用)。

(继承自 FileDialog)
Options

获取要初始化的值 FileDialog

(继承自 FileDialog)
ReadOnlyChecked

获取或设置一个值,该值指示是否选中只读复选框。

RestoreDirectory

获取或设置一个值,该值指示对话框是否在关闭之前将目录还原到以前选定的目录。

(继承自 FileDialog)
SafeFileName

获取对话框中所选文件的文件名和扩展名。 文件名不包括路径。

SafeFileNames

获取对话框中所有选定文件的文件名和扩展名数组。 文件名不包括路径。

SelectReadOnly

获取或设置一个值,该值指示对话框是否允许选择只读文件。

ShowHelp

获取或设置一个值,该值指示“ 帮助 ”按钮是否显示在文件对话框中。

(继承自 FileDialog)
ShowHiddenFiles

获取或设置一个值,该值指示对话框是否显示隐藏文件和系统文件。

(继承自 FileDialog)
ShowPinnedPlaces

获取或设置一个值,该值指示是否显示视图导航窗格中默认显示的项目。

(继承自 FileDialog)
ShowPreview

获取或设置一个值,该值指示对话框是否显示所选文件的预览。

ShowReadOnly

获取或设置一个值,该值指示对话框是否包含只读复选框。

Site

获取或设置 ISite .Component

(继承自 Component)
SupportMultiDottedExtensions

获取或设置对话框是否支持显示和保存具有多个文件扩展名的文件。

(继承自 FileDialog)
Tag

获取或设置一个对象,该对象包含有关控件的数据。

(继承自 CommonDialog)
Title

获取或设置文件对话框标题。

(继承自 FileDialog)
ValidateNames

获取或设置一个值,该值指示对话框是否只接受有效的 Win32 文件名。

(继承自 FileDialog)

方法

名称 说明
CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放该 Component命令使用的所有资源。

(继承自 Component)
Dispose(Boolean)

释放由托管资源使用 Component 的非托管资源,并选择性地释放托管资源。

(继承自 Component)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetService(Type)

返回一个对象,该对象表示服务由 Component 或其 Container提供的服务。

(继承自 Component)
GetType()

获取当前实例的 Type

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

定义重写的通用对话框挂钩过程,以向文件对话框添加特定功能。

(继承自 FileDialog)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
OnFileOk(CancelEventArgs)

引发 FileOk 事件。

(继承自 FileDialog)
OnHelpRequest(EventArgs)

引发 HelpRequest 事件。

(继承自 CommonDialog)
OpenFile()

使用只读权限打开用户选择的文件。 该文件由 FileName 属性指定。

OwnerWndProc(IntPtr, Int32, IntPtr, IntPtr)

定义重写的所有者窗口过程,以向通用对话框添加特定功能。

(继承自 CommonDialog)
Reset()

将所有属性重置为其默认值。

RunDialog(IntPtr)

指定一个通用对话框。

(继承自 FileDialog)
ShowDialog()

运行具有默认所有者的通用对话框。

(继承自 CommonDialog)
ShowDialog(IWin32Window)

运行具有指定所有者的公用对话框。

(继承自 CommonDialog)
ToString()

提供此对象的字符串版本。

(继承自 FileDialog)

活动

名称 说明
Disposed

当组件通过对方法的调用 Dispose() 释放时发生。

(继承自 Component)
FileOk

当用户单击文件对话框中的“ 打开 ”或“ 保存 ”按钮时发生。

(继承自 FileDialog)
HelpRequest

当用户单击常见对话框中的“帮助”按钮时发生。

(继承自 CommonDialog)

适用于

另请参阅