方式1: 使用win32控件OpenFileDialog


Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); 

ofd.DefaultExt = ".xml"; 

ofd.Filter = "xml file|*.xml"; 

if (ofd.ShowDialog() == true) 

{ 

     //此处做你想做的事 ...=ofd.FileName; 

} 

方式2: 使用Forms中的OpenFileDialog控件

WPF中是不能直接使用Forms中的控件的,否则会提示找不到Forms名字控件. 如果你仍然要用, 答案便是添加.net 引用reference

System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 

openFileDialog1.InitialDirectory = "c:\\"; 

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 

openFileDialog1.FilterIndex = ; 

openFileDialog1.RestoreDirectory = true; 

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 

{ 

    //此处做你想做的事 ...=openFileDialog1.FileName; 

}

类似的有文件夹浏览对话框:

System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); 

System.Windows.Forms.DialogResult result = folderBrowserDialog.ShowDialog(); 

if (result == System.Windows.Forms.DialogResult.OK) 

{ 

    tb_FolderPath.Text = folderBrowserDialog.SelectedPath; 

} 

方式三: 使用win32 api

BOOL WINAPI GetOpenFileName(  __inout  LPOPENFILENAME lpofn)

使用这种方式, 你需要自己实现LPOPENFILENAME结构和对GetOpenFileName方法就行封装:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 

public class OpenFileName 

{ 

    public int structSize = ; 

    public IntPtr hwnd = IntPtr.Zero; 

    public IntPtr hinst = IntPtr.Zero; 

    public string filter = null; 

    public string custFilter = null; 

    public int custFilterMax = ; 

    public int filterIndex = ; 

    public string file = null; 

    public int maxFile = ; 

    public string fileTitle = null; 

    public int maxFileTitle = ; 

    public string initialDir = null; 

    public string title = null; 

    public int flags = ; 

    public short fileOffset = ; 

    public short fileExtMax = ; 

    public string defExt = null; 

    public int custData = ; 

    public IntPtr pHook = IntPtr.Zero; 

    public string template = null; 

} 

public class LibWrap 

{ 

    // Declare a managed prototype for the unmanaged function. 

    [DllImport("Comdlg32.dll",SetLastError=true,ThrowOnUnmappableChar=true, CharSet = CharSet.Auto)] 

    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn); 

}

之后的工作就是实例化、初始化和方法调用了:

OpenFileName ofn = new OpenFileName(); 

ofn.structSize = Marshal.SizeOf(ofn); 

ofn.filter = "Project files\0*.xml"; 

ofn.file = new string(new char[]); 

ofn.maxFile = ofn.file.Length; 

ofn.fileTitle = new string(new char[]); 

ofn.maxFileTitle = ofn.fileTitle.Length; 

ofn.initialDir = "C:\\"; 

ofn.title = "Open Project"; 

ofn.defExt = "xml"; 

ofn.structSize = Marshal.SizeOf(ofn); 

if (LibWrap.GetOpenFileName(ofn)) 

{ 

    //此处做你想做的事 ...=ofn.file; 

} 

WPFの操作文件浏览框几种方式的更多相关文章

  1. 文件对话框WPF(5)----文件浏览对话框

    废话就不多说了,开始... WPF中文件浏览对话框的实现可以利用Windows API Code Pack,它是一个用于访问Windows Vista/7 特性的托管代码函数库,但并没有包含在.NET ...

  2. Linux经常使用的文件传输的几种方式

    Linux经常使用的文件传输的几种方式 1.终端新建stfp协议连接 或者命令方式: sftp -P22 root@192.168.11.100 端口可以不用填写,默认是22,端口的P是大写. 将本地 ...

  3. Django文件上传三种方式以及简单预览功能

    主要内容: 一.文件长传的三种方式 二.简单预览功能实现 一.form表单上传 1.页面代码 <!DOCTYPE html> <html lang="en"> ...

  4. 转载:删除github上文件夹的两种方式

    http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...

  5. PHP读取文件内容的五种方式(转载)

    php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp); php读取文件内容: -----第一种方法--- ...

  6. python学习之文件读写入门(文件读的几种方式比较)

    1.文件读写简单实例:(以w写的方式打开一个文件,以r读一个文件) # Author : xiajinqi # 文件读写的几种方式 # 文件读写 f = open("D://test.txt ...

  7. Java实现文件复制的四种方式

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...

  8. java文件读写的两种方式

    今天搞了下java文件的读写,自己也总结了一下,但是不全,只有两种方式,先直接看代码: public static void main(String[] args) throws IOExceptio ...

  9. 办公室的远程传文件 的命令三种方式linux

    不同的Linux之间copy文件常用有3种方法: 第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy. 第二种方法就是 ...

随机推荐

  1. JSP+Servlet+mysql简单示例【图文教程】

    下载MYSQL:http://dev.mysql.com/downloads/ 下载安装版的 然后安装(安装步骤就不详细说了) 安装好之后,点击托盘图标,打开管理工具 创建一个数据库   数据库的名字 ...

  2. PXE | 开关机

    PXE | 开关机流程 linuxPXE 主要阶段 引导的主要6个阶段 从MBR中读取引导加载程序boot loader 加载并初始化内核: 检测和配置设备: 创建内核进程: 系统管理员干预(单用户模 ...

  3. shell | {}和()

    执行 bash -n xx.sh用于检测脚本语法是否有错误 bash -x xx.sh用于追踪执行 ${var} 用于限定变量名称的范围,并且支持通配符 $(cmd) shell会先执行括号的cmd, ...

  4. [NOI 2017]整数

    Description 题库链接 P 博士将他的计算任务抽象为对一个整数的操作. 具体来说,有一个整数 \(x\) ,一开始为 \(0\) . 接下来有 \(n\) 个操作,每个操作都是以下两种类型中 ...

  5. Django学习(5)优雅地分页展示网页

    在我们平时浏览网页时,经常会遇到网页里条目很多的情形,这时就会用到分页展示的功能.那么,在Django中,是如何实现网页分类的功能的呢?答案是Paginator类. 本次分享讲具体展示如何利用Djan ...

  6. H5实现魔方游戏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【Java并发编程】9、非阻塞同步算法与CAS(Compare and Swap)无锁算法

    转自:http://www.cnblogs.com/Mainz/p/3546347.html?utm_source=tuicool&utm_medium=referral 锁(lock)的代价 ...

  8. 【CSS学习】--- 文本样式

    一.前言 CSS文本属性可以定义文本的外观.通过文本属性,可以定义文本的颜色.字符间距,对齐文本,装饰文本,对文本进行缩进,等等. CSS常用的文本属性目录: text-align 文本对齐属性 te ...

  9. JS的一些小知识

    1.     0.1+0.2==0.3? 结果为false 原因:由于计算机是用二进制来存储和处理数据数字,不能精确表示浮点数,而JavaScript是一种弱类型语言,没有相应的封装类来处理浮点数运算 ...

  10. python之类与类之间的关系

    在我们的世界中事物和事物之间总会有一些联系.   在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻的. 随时可以 ...