WPF(OpenFileDialog)打开文件对话框的设置

时间:2021-08-01 12:17:05

常见的“打开文件”对话框实现为 OpenFileDialog 类,位于 Microsoft.Win32 命名空间中。 下面的代码演示如何创建、配置和显示“打开文件”对话框以及如何处理结果。

// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
dialog.Filter = "图片文件(.jpg;.bmp;.png)|*.jpg;*.bmp;*.png|所有文件|*.*";//another method to set filter
 
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
 
// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
}