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

常见的“打开文件”对话框实现为 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;
}
posted @ 2018-03-31 16:42  死鱼眼の猫  阅读(527)  评论(0编辑  收藏  举报