OpenFileDialog 类
提示用户打开文件。无法继承此类。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public NotInheritable Class OpenFileDialog
Inherits FileDialog
用法
Dim instance As OpenFileDialog
public sealed class OpenFileDialog : FileDialog
public ref class OpenFileDialog sealed : public FileDialog
public final class OpenFileDialog extends FileDialog
public final class OpenFileDialog extends FileDialog
备注
使用此类可检查某个文件是否存在并打开该文件。ShowReadOnly 属性确定是否在对话框中显示只读复选框。ReadOnlyChecked 属性指示是否选中只读复选框。
此类的大多数功能都可以在 FileDialog 类中找到。
如果要使用户能够选择文件夹而不是文件,请改用 FolderBrowserDialog。
示例
下面的代码示例创建一个 OpenFileDialog,设置几个属性,并使用 CommonDialog.ShowDialog 方法显示此对话框。该示例要求窗体上放置了一个 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
线程安全
此类型的任何公共静态(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
请参见
参考
OpenFileDialog 成员
System.Windows.Forms 命名空间
FileDialog 类
CommonDialog 类
SaveFileDialog