原文 在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. 1410 - Consistent Verdicts(规律)

    1410 - Consistent Verdicts   PDF (English) Statistics Forum Time Limit: 5 second(s) Memory Limit: 32 ...

  2. Week11(11月21日)

    Part I:提问 =========================== 1.如何编辑更新一条记录? Part II:案例学习 =========================== MusicSt ...

  3. 【HTTP 2】HTTP/2 协议概述(HTTP/2 Protocol Overview)

    前情提要 在上一篇文章<[HTTP 2.0] 简介(Introduction)>中,我们简单介绍了 HTTP 2. 在本篇文章中,我们将会了解到 HTTP 2 协议概述部分的内容. HTT ...

  4. UVALive 5797 In Braille

    题目 比赛总结 题意:给出1-9的盲文,每种盲文都是2×3的点阵,有些点是凸起的用*表示,其余的用.表示.要进行两种操作,1 把盲文变成数字,2 把数字变成盲文 解法:按规则模拟....注意读入的每个 ...

  5. CAN总线基础

    can总线协议: 涵盖了OSI规定的传输层.数据链路层.物理层 物理层: 决定了位编码方式(NRZ编码,6个位插入填充位),位时序(位时序.位的采样).同步方式(根据同步段ss实现同步,并具有再同步功 ...

  6. Cocos2D-X扫盲之坐标系、锚点

    一.引言 在Cocos2D-X的开发过程中,经常会碰到设置精灵位置的问题.而设置位置的过程,涉及到两个问题:第一是坐标系,包括原点的位置.X/Y坐标轴的方向灯:第二是基准点(Cocos2D-X中叫锚点 ...

  7. SQL Server 基础 04 函数与分组查询数据

    函数与分组查询数据 系统函数分 聚合函数.数据类型转换函数.日期函数.数学函数 . . . 1. 聚合函数 主要是对一组值进行计算,然后返回一个值. 聚合函数包括 sum(求和).avg(求平均值). ...

  8. ZOJ 3609 求逆元

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  9. javascript笔记整理(事件)

    一.事件驱动 1.事件javascript侦测到的用户的操作或是页面的一些行为(怎么发生的) 2.事件源引发事件的元素(发生在谁的身上) 3.事件处理程序对事件处理的程序或是函数 (发生了什么事) 二 ...

  10. 分享非常有用的Java程序 (关键代码) (二)---列出文件和目录

    原文:分享非常有用的Java程序 (关键代码) (二)---列出文件和目录 File dir = new File("directoryName"); String[] child ...