一、OpenFileDialog
OpenFileDialog openFileDialog; Nullable<bool> b; public MainWindow() { InitializeComponent(); } private void btn_Click(object sender, RoutedEventArgs e) { openFileDialog = new OpenFileDialog(); //默认名称 openFileDialog.FileName = "file"; //弹出框标题名称,默认 "打开" openFileDialog.Title = "读取文档"; //过滤器 openFileDialog.Filter = "图像文件(*.bmp, *.jpg)|*.bmp;*.jpg|word文档(*.doc)|*.doc|所有文件(*.*)|*.*"; //默认打开路径 openFileDialog.InitialDirectory = "d:"; //注册点击打开事件 //openFileDialog.FileOk += Open_Click; //点击打开返回true,取消返回false b = openFileDialog.ShowDialog(); if (b == true) { string file = openFileDialog.FileName; MessageBox.Show("你选择的文件是:" + file.ToString()); } MessageBox.Show(b.ToString()); } //public void Open_Click(object sender, CancelEventArgs e) //{ // if (b == true) // { // string file = openFileDialog.FileName; // MessageBox.Show("你选择的文件是:" + file.ToString()); // } // else // { // MessageBox.Show(b.ToString()); // } //}
发现不需要用事件触发,ShowDialog会返回一个bool值
二、SaveFileDialog
同上