原文 在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

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

  1. C# winform 组件---- folderBrowserDialog与openFileDialog(转)

    C# winform 组件---- folderBrowserDialog与openFileDialog 2009-06-27 13:36 2153人阅读 评论(1) 收藏 举报 winformc#b ...

  2. WPF - 使用Microsoft.Win32.OpenFileDialog打开文件,使用Microsoft.Win32.SaveFileDialog将文件另存

    1. WPF 使用这个方法打开文件,很方便,而且可以记住上次打开的路径. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.W ...

  3. C# 選擇本機檔案並上傳

    參考自:http://www.dotblogs.com.tw/puma/archive/2008/11/07/5910.aspxhttp://www.codeproject.com/Articles/ ...

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

    wpf: Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = &qu ...

  5. Excel另存为_有些Excel打开时会出现一些提示

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

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

    1.前言 WPF并没有文件选择对话框,要用也就只有使用Winform版的控件.至今我也没有寻找到一个WPF版本的文件选择对话框. 可能是我眼浊,如果各位知道有功能比较健全的WPF版文件选择对话框.文件 ...

  7. WPF 中的OpenFileDialog和 OpenFolderDialog

    OpenFolderDialog: using (var dialog = new System.Windows.Forms.FolderBrowserDialog() { SelectedPath ...

  8. .net core 3.0 WPF中使用FolderBrowserDialog

    前言 随着.net core 3.0 的发布,WPF 也可以在 core 平台上使用了.当前的 WPF 不支持跨平台,仅能够在 Windows 平台上使用.如果想体验 WPF 跨平台开发,可以访问开源 ...

  9. 使用OpenFileDialog打开文件和使用FolderBrowserDialog选定文件夹

    选定文件夹 string foldPath = ""; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog ...

随机推荐

  1. 静态化 - 伪静态技术(PHP正则表达式实现)

    效果: 代码: <?php // + —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— —— // + 正则表达式,实 ...

  2. grub2的/etc/grub.d目录下的脚本文件

    00_header,05_debian_theme,10_linux,20_memtest86+,30_os- prober,40_custom这五个脚本对应grub.cfg上的各个部分,有的版本的g ...

  3. 2014 HDU多校弟五场J题 【矩阵乘积】

    题意很简单,就是两个大矩阵相乘,然后求乘积. 用 Strassen算法 的话,当N的规模达到100左右就会StackOverFlow了 况且输入的数据范围可达到800,如果变量还不用全局变量的话连内存 ...

  4. json文件问题

    在json数据没有错误 字符 的前提下  出现的问题 如果有中文 那么编码格式 必须是UTF-8格式的

  5. Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明(转)

    一.  官网说明 在DBCA 建库的时候,有提示让我们选择连接类型,这里有两种类型:专用服务器模式和共享服务器模式.默认使用专用模式.如下图: Oracle 官方文档对这两种文档的说明如下: Abou ...

  6. 基于visual Studio2013解决C语言竞赛题之0509杨辉三角

     题目

  7. 什么是TimeTunnel

    index - Taocode 欢迎使用TimeTunnel PageOutline(1-3,,inline) 什么是TimeTunnel !TimeTunnel(简称TT)是一个基于thrift通讯 ...

  8. 将EC2里的实例导出到RAW文件并进行修改

    你可能有自己的instance在amazon云环境里面,或者是你想深度修改一下marketplace里面提供的那些系统又估计运行中的instance改动不方便 亚马逊作为云计算领域的大哥大,我不得不说 ...

  9. PHP - 对象转json - json转数组

    前台js转为json,传给php后台,php后台接收并转为数组. 效果: -- 前台js将对象转为json: var rows = JSON.stringify(rows); 后台php接收转为数组: ...

  10. 一、Nginx配置文件详解

    配置文件介绍 主要有两部分:分别是 main:主体部分 http{}:虚拟主机配置部分 配置指令主要以分号结尾:配置语法:directive value1 [value2 ....] 支持使用的变量 ...