首页 > Windows开发 > 详细

WPF选择文件和文件夹对话框

时间:2018-12-20 12:50:58      阅读:287      评论:0      收藏:0      [点我收藏+]

WPF提供了选择文件对话框,但并没有提供选择文件夹的对话框。

OpenFileDialog类存在于PresentationFramework.dll程序集。
 1 public string SelectFileWpf()
 2         {
 3             var openFileDialog = new Microsoft.Win32.OpenFileDialog()
 4             {
 5                 Filter = "Text documents (.txt)|*.txt|All files (*.*)|*.*"
 6             };
 7             var result = openFileDialog.ShowDialog();
 8             if (result == true)
 9             {
10                 return openFileDialog.FileName;
11             }
12             else
13             {
14                 return null;
15             }
16         }

 

下面需要添加System.Windows.Forms.dll

using System.Windows.Forms;
1  public string SelectPath() //弹出一个选择目录的对话框
2         {
3             FolderBrowserDialog path = new FolderBrowserDialog();
4             path.ShowDialog();
5             return path.SelectedPath;
6         }

 

选择文件

1  public string SelectFile() //弹出一个选择文件的对话框
2         {
3             OpenFileDialog file = new OpenFileDialog();
4             file.ShowDialog();
5             return file.SafeFileName;
6         }

 

参考:

https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.openfiledialog?view=netframework-4.7.2

WPF选择文件和文件夹对话框

原文:https://www.cnblogs.com/xinyf/p/10148515.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!