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
下面的代码示例创建一个、设置多个
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
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
。
获取或设置要与此对话框状态关联的 GUID。 通常情况下,状态(如最后访问的文件夹)和对话框的位置及大小将根据可执行文件的名称持久保存。 通过指定 GUID,一个应用程序对于同一应用程序中不同版本的对话框(例如,导入的对话框和打开的对话框),可以具有不同的持久状态。
如果应用程序未使用视觉样式或如果
AutoUpgradeEnabled
设置为
false
,则此功能不可用。
(继承自
FileDialog
)