在WPF使用FolderBrowserDialog和OpenFileDialog。

相信习惯以前winform开发的朋友们都对FolderBrowserDialog和OpenFileDialog这两个东东不陌生,但是在我最近做的WPF项目中

才发现这两个东东在WPF中却不是默认存在的,郁闷,好歹WPF也出来几年了,咋个微软的同志不与时俱进呢。

好了,说说具体怎么用吧。

OpenFileDialog

用这个东东需要引用Microsoft.Win32类库。还是老玩意可靠。

Microsoft.Win32.OpenFileDialog op =
new Microsoft.Win32.OpenFileDialog();
         op.InitialDirectory = @"c:\";
          op.RestoreDirectory =
true;           op.Filter =
"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
          op.ShowDialog();
          txtPath.Text =
op.FileName;

FolderBrowserDialog

这个要麻烦点点,先建一个类,比如命名为OldWindow.cs

public class OldWindow :
System.Windows.Forms.IWin32Window   {       IntPtr
_handle;       public OldWindow(IntPtr handle)
      {
          _handle = handle;
      }

#region IWin32Window Members       IntPtr
System.Windows.Forms.IWin32Window.Handle       {
          get { return _handle; }
      }       #endregion
  }

然后在你要使用的地方这样写

System.Windows.Forms.FolderBrowserDialog dlg =
new System.Windows.Forms.FolderBrowserDialog();
        
System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this)
as System.Windows.Interop.HwndSource;
        
System.Windows.Forms.IWin32Window win = new
{上面那个类所在的命名空间名称}.OldWindow(source.Handle);
        
System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);

txtPath.Text = dlg.SelectedPath;

BTW:需要在项目中引用System.Windows.Forms.dll

摘自:http://www.cnblogs.com/allancandy/archive/2010/11/19/1882062.html

时间: 2024-12-17 04:24:11

在WPF使用FolderBrowserDialog和OpenFileDialog。的相关文章

WPF - 使用Microsoft.Win32.OpenFileDialog打开文件

WPF 使用这个方法打开文件,很方便,而且可以记住上次打开的路径. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Multiselect = false; openFileDialog.Filter = "AllFiles|*.*"; if ((bool)openFileDialog.ShowDialog()) { txtPath.

C#三个平台上的文件选择方法

wpf: Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = ".txt"; Nullable<bool> result = dlg.ShowDialog(); if (result == true) { // Open document string filename = dlg.FileName; } FileStream aFile =

Wpf OpenFileDialog

Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog(); openFileDialog1.Filter = "(*.cup)|*.cup"; if (openFileDialog1.ShowDialog() == true) { MessageBox.Show(openFileDialog1.FileName); } 来自为知笔记(Wiz) Wpf OpenFileDia

WPF:自定义Metro样式文件夹选择对话框FolderBrowserDialog

1.前言 WPF并没有文件选择对话框,要用也就只有使用Winform版的控件.至今我也没有寻找到一个WPF版本的文件选择对话框. 可能是我眼浊,如果各位知道有功能比较健全的WPF版文件选择对话框.文件打开对话框,还请留言告知. 这次做的是一个精简版的文件选择对话框.包含一个UserControl和一个承载UserControl的Window. 另外TreeView的样式引用自Mahspps中的样式.也就是如果需要使用这个文件选择对话框,就必须要引用Mahapps的相关dll. 当然,我会提供整个

.net core 3.0 WPF中使用FolderBrowserDialog

前言 随着.net core 3.0 的发布,WPF 也可以在 core 平台上使用了.当前的 WPF 不支持跨平台,仅能够在 Windows 平台上使用.如果想体验 WPF 跨平台开发,可以访问开源项目Avalonia.不过当前的 WPF 已经可以满足我们的大部分使用需求了,毕竟使用 core 开发起来很爽.这意味着不必在用户的机器上安装 .net framework 依赖环境,以独立的方式发布的软件,复制到任意一台 Windows 上就可以直接运行. 启程 当我们带着激动的心情开始新的 WP

C#选择文件(OpenFileDialog)、选择文件夹(FolderBrowserDialog)

1  选择文件(OpenFileDialog) OpenFileDialog dialog = new OpenFileDialog(); //dialog.Multiselect = true;//该值确定是否可以选择多个文件 // dialog.Title = "请选择文件"; //dialog.Filter = "所有文件(*.*)|*.*"; //dialog.InitialDirectory = @"E:\"; if (dialog.S

在WPF中使用文件夹选择对话框

开发中有时会想实现"选择某个文件夹"的效果: 在WPF中,使用Microsoft.Win32.OpenFileDialog只能选择文件,FolderBrowserDialog只能用树型的方式选择文件夹,很不好用. 终于找到一个办法,使用Windows API Code Pack 在VS里打开Package Manager Console后输入Install-Package WindowsAPICodePack-Shell获取包后 就可以像这样打开选择文件夹Dialog了: var di

wpf打开文夹和打开文件

WPF调用WinForm中的 OpenFileDialog 和 FolderBrowserDialog 来实现响应的功能 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "选择文件"; openFileDialog.Filter = "zip文件|*.zip|rar文件|*.rar|所有文件|*.*"; openFileDialog.FileName = str

WPF文件和文件夹的操作

1.对文件的操作 private void button_chose_Click(object sender, RoutedEventArgs e) { var openFileDialog = new Microsoft.Win32.OpenFileDialog() { Filter = "Excel Files (*.sql)|*.sql" }; var result = openFileDialog.ShowDialog(); if (result == true) { this