原文 在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. struts2的总体回想(ACTION、拦截器、值栈、OGNL表达式、ModelDriven方案等)

    ValueStack:struts2的一个存放数据的数据结构(核心) ValueStack大致能够理解为:由Map和对象栈组成 ValueStack作用范围:一个请求,用它来取代request的作用域 ...

  2. Windows 下统计行数的命令

    大家都知道在Linux下统计文本行数能够用wc -l 命令.比如: -bash-3.2$ cat pif_install.log | wc -l       712 但在Windows下怎样统计输出文 ...

  3. <climits>头文件使用方法

    <climits>头文件定义的符号常量 CHAR_MIN  char的最小值SCHAR_MAX  signed char 最大值SCHAR_MIN   signed char 最小值UCH ...

  4. Windows Phone 8初学者开发—第19部分:设置RecordAudio.xaml页面

    原文 Windows Phone 8初学者开发—第19部分:设置RecordAudio.xaml页面 原文地址:  http://channel9.msdn.com/Series/Windows-Ph ...

  5. [置顶] jeecg-framework-3.3.2-RELEASE 最新版本发布

      平台介绍 JEECG(J2EE CodeGeneration)是一款基于代码生成器的智能开发平台,引领新开发模式(OnlineCoding模式->代码生成器模式->手工MERGE智能开 ...

  6. 关于DataGridViewComboBoxCell修改后提交数据源

    最近在项目遇到一个功能实现.是在DataGridView中DataGridViewComboboxColumn列绑定数据源, DisplayMember为数据表的Name列,ValueMember是数 ...

  7. 由于物化视图定义为on commit导致update更新基表慢的解决方案

    由于物化视图定义为on commit导致update更新基表慢的解决方案 以下是模拟和解决测试过程: (模拟update慢的过程) 1.首先基于基表创建物化视图日志: create materiali ...

  8. c#取出LDAP SearchResult所有属性

    string aaa = System.Threading.Thread.CurrentPrincipal.Identity.Name; DirectorySearcher ds = new Dire ...

  9. Android学习笔记之View(二)

    View加载的流程之测量:rootView调用measure()→onMeasure(): measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以on ...

  10. CheckBox控件

    前台代码: <asp:CheckBox ID="CheckBox1" runat="server" Text ="苹果"/> & ...