OpenFileDialog 类
定义
命名空间:
System.Windows.Forms
程序集:System.Windows.Forms.dll
Source:OpenFileDialog.cs
Source:OpenFileDialog.cs
Source:OpenFileDialog.cs
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
显示一个标准对话框,提示用户打开文件。 此类不能被继承。
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
继承
Object
MarshalByRefObject
Component
CommonDialog
FileDialog
OpenFileDialog
示例
下面的代码示例创建 一个 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,一个应用程序对于同一应用程序中不同版本的对话框(例如,导入的对话框和打开的对话框),可以具有不同的持久状态。
如果应用程序未使用视觉样式或如果 AutoUpgradeEnabled 设置为 false,则此功能不可用。
(继承自 FileDialog)
Container
获取包含 IContainer 的 Component。
(继承自 Component)
CustomPlaces
获取此 FileDialog 实例的自定义空间的集合。
(继承自 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
获取或设置 Component 的 ISite。
(继承自 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)
适用于
另请参阅
FileDialog
CommonDialog
SaveFileDialog
如何:从文件中读取文本