WPF中,调用OpenFileDialog时有两个选择:

  1. System.Windows.Forms.OpenFileDialog 

  2. Microsoft.Win32.OpenFileDialog


    使用System.Windows.Forms.OpenFileDialog时,ShowDialog()的返回值是DialogResult的枚举值. 如果返回值是DialogResult.OK, 可以通过调用OpenFileDialog的对象中的FileName属性来得到在ShowDialog()中选中的文件.

示例代码:

   System.Windows.Forms.OpenFileDialog op = new System.Windows.Forms.OpenFileDialog();
            op.Multiselect = false;
            op.AddExtension = true;
            op.DereferenceLinks = true;
            if (op.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                Target = op.FileName;

    使用Microsoft.Win32.OpenFileDialog时,ShowDialog()的返回值是可空类型bool?(true,false,null),不能在if中直接判断,而是要用"=="来判断.

示例代码:

Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
if(op.ShowDialog()==true) Target = op.FileName;