方式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. [译]ABP vNext介绍

    译者注 ASP.NET Boilerplate是.Net平台非常优秀的一个开源Web应用程序框架,在国内也有大量的粉丝. 近日, 本人在github上闲逛, 发现ASP.NET Boilerplate ...

  2. DataAnnotations - InverseProperty Attribute:

    DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that ...

  3. IBatisNet动态update以及DateTime类型字段处理

    在维护一个老项目中碰到的问题.SQL配置如下(只简单列出两个字段): <update id="ProjectInfo.Update" parameterClass=" ...

  4. ubuntu16.04LTS更换阿里源

    ubuntu16.04LTS更换阿里源 sudo gedit /etc/apt/sources.list 替换:    # deb cdrom:[Ubuntu 16.04 LTS _Xenial Xe ...

  5. 23种设计模式+J2EE设计模式学习笔记-初识设计模式

    设计模式简介: 设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验的总结.(个人理解:设计模式是不关乎业务,逻辑实现,针对普遍问题的一种解决方案). 设计模式的类型: 传统23种设 ...

  6. 为什么 C# 比 C++ 编译快那么多

    Go 我不懂,下面以 C++ 和 C# 对比来说明为什么 C++ 编译慢和 C# 编译快. C 和 C++ 文件的编译经过几个主要步骤: 处理续行符处理(“\”)之类的杂事 词法分析,解析出 toke ...

  7. modifyGeoJSON

    from osgeo import ogr import json from geojson import loads, dumps, Feature, FeatureCollection from ...

  8. Android之ProgressDialog的使用

    ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口. ProgressDialog的创建方式有两种,一种是ne ...

  9. node中__dirname、__filename、process.cwd()、process.chdir()表示的路径

    直接上结论:__dirname 表示当前文件所在的目录的绝对路径__filename 表示当前文件的绝对路径module.filename ==== __filename 等价process.cwd( ...

  10. beta冲刺随笔集

    团队成员 郑西坤 031602542 (队长) 陈俊杰 031602504 陈顺兴 031602505 张胜男 031602540 廖钰萍 031602323 雷光游 031602319 吴志鸿 03 ...