WPF中使用文件浏览对话框的几种方式
WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式
方式1: 使用win32控件OpenFileDialog
1
2
3
4
5
6
7
|
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); ofd.DefaultExt = ".xml" ; ofd.Filter = "xml file|*.xml" ; if (ofd.ShowDialog() == true ) { //此处做你想做的事 ...=ofd.FileName; } |
与之类似的还有 Microsoft.Win32.SaveFileDialog
方式2: 使用Forms中的OpenFileDialog控件
WPF中是不能直接使用Forms中的控件的,否则会提示找不到Forms名字控件. 如果你仍然要用, 答案便是添加.net 引用reference
之后就可以像下面一样正常使用Forms中的控件了
1
2
3
4
5
6
7
8
9
|
System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true ; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //此处做你想做的事 ...=openFileDialog1.FileName; } |
类似的有文件夹浏览对话框:
1
2
3
4
5
6
|
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方法就行封装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OpenFileName { public int structSize = 0; public IntPtr hwnd = IntPtr.Zero; public IntPtr hinst = IntPtr.Zero; public string filter = null ; public string custFilter = null ; public int custFilterMax = 0; public int filterIndex = 0; public string file = null ; public int maxFile = 0; public string fileTitle = null ; public int maxFileTitle = 0; public string initialDir = null ; public string title = null ; public int flags = 0; public short fileOffset = 0; public short fileExtMax = 0; public string defExt = null ; public int custData = 0; 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); } |
之后的工作就是实例化、初始化和方法调用了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
OpenFileName ofn = new OpenFileName(); ofn.structSize = Marshal.SizeOf(ofn); ofn.filter = "Project files\0*.xml" ; ofn.file = new string ( new char [256]); ofn.maxFile = ofn.file.Length; ofn.fileTitle = new string ( new char [64]); 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; } |
方式四: 自己写控件(王道)或者使用第三方控件
第一种方式由于不能自己输入路径, 只能通过鼠标在treeview中进行选择, 十分的不喜欢;第三种稍微复杂;第四种有待考察,听说的第三方有SystemWrapper和WAF; 所以暂时选择第二种.
WPF中使用文件浏览对话框的几种方式的更多相关文章
- [转]Windows系统中监控文件复制操作的几种方式
1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...
- Windows系统中监控文件复制操作的几种方式
http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...
- WPF中控制窗口显示位置的三种方式
首先新建一个WPF工程,在主界面添加一个按钮,并给按钮添加点击事件button1_Click,然后新建一个用于测试弹出位置的窗口TestWindow.1.在屏幕中间显示,设置window.Window ...
- ASP.NET Core 1.0中实现文件上传的两种方式(提交表单和采用AJAX)
Bipin Joshi (http://www.binaryintellect.net/articles/f1cee257-378a-42c1-9f2f-075a3aed1d98.aspx) Uplo ...
- 文件对话框WPF(5)----文件浏览对话框
废话就不多说了,开始... WPF中文件浏览对话框的实现可以利用Windows API Code Pack,它是一个用于访问Windows Vista/7 特性的托管代码函数库,但并没有包含在.NET ...
- WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...
- WPF 中style文件的引用
原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment. ...
- JAVA中获取文件MD5值的四种方法
JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...
- linux中删除文件内空白行的几种方法。
linux中删除文件内空白行的几种方法 有时你可能需要在 Linux 中删除某个文件中的空行.如果是的,你可以使用下面方法中的其中一个.有很多方法可以做到,但我在这里只是列举一些简单的方法. 你可能已 ...
随机推荐
- SDL2.0教程翻译·目录
原文地址:SDL 2.0 Tutorial Index Welcome! 下面的教程旨在为你提供一个SDL2.0以及c++中游戏设计和相关概念的介绍.在本教程中,我们假定你对C++有一定程度上的知识, ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- Sublime Text 3配置记录
G++ /** * 工具->编译系统->新编译系统 */ { "cmd": ["g++", "${file}", "- ...
- 执行npm安装模块的命令 Cannot find module
npm 安装了 appium 和 appium-doctor 运行命令,appium-doctor 提示找不到模块: C:\Users\autotest>appiummodule.js:471 ...
- Linux命令之exit
本文链接:http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...
- [AngularJS] Angular 1.5 $transclude with named slot
In Angular 1.5, there is no link and compile. So use if you transclude, you cannot access the fifth ...
- Netmon: A light-weight network monitor for Windows
Netmon is a light-weight network monitor that works on Windows operating systems. It provides differ ...
- 使用Ksoap2调用Web Service加入SoapHeader
关于这个问题,如果使用百度都是前篇一律的代码,好不容易上了google才找到完整的方法,这里讲所有的代码都贴出来与大家分享. 首先是.NET写的后台代码 /// <summary> /// ...
- css3选择符使用个人理解。
元素选择符: 通配选择符:一般用* 星号表示 他会命中整体标签. 类型选择符:一般用者空格表示 他会命中标签自己的所有子元素. ID选择符: 一般用# 井号表示 他会命中以id为属性的标签 ...
- 【socket.io研究】0.前提准备
WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...