WPF中使用文件浏览对话框的几种方式

简介: 原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用win32控件OpenFileDialog ? 1 2 3 4 5 6 7 Microsoft.
原文: 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; 所以暂时选择第二种.

目录
相关文章
|
C# 前端开发
WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用——使用xaml代码或者C#代码动态加载   WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观和行为,如同CSS代码一般。
4532 0
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
|
C#
WPF中查看PDF文件
原文:WPF中查看PDF文件 需要打开PDF文件时,我们第一印象就是使用Adobe Reader。在开发中,经常会遇到需要展示PDF文件的需求。我们会借助于Adobe Reader的Active控件来实现。
1671 0
|
XML C# 数据格式
WPFの操作文件浏览框几种方式
原文:WPFの操作文件浏览框几种方式 方式1: 使用win32控件OpenFileDialog Microsoft.Win32.OpenFileDialog ofd = new Microsoft.
1039 0
|
C# 数据安全/隐私保护 缓存
使用WPF将图片转变为灰度并加上水印并保存为文件
原文:使用WPF将图片转变为灰度并加上水印并保存为文件 运行效果: (上图中左下角为原图的缩小显示,By:Johnson为TextBlock)保存的结果图片:上图的“Test Words.”为水印文字。
1001 0
|
C#
WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片
原文:WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片 1、图片文件转换成Visual对象         private Visual CreateVisual(string imageFileName)        {            BitmapImag...
1157 0
|
C#
WPF 3D动态加载模型文件
原文:WPF 3D动态加载模型文件 这篇文章需要读者对WPF 3D有一个基本了解,至少看过官方的MSDN例子。 一般来说关于WPF使用3D的例子,都是下面的流程: 1.美工用3DMAX做好模型,生成一个obj文件 2.程序然后打开Blender,将obj拖动到Blender中,生成xaml代码   但是这样做会有至少两个问题: 1. 维护麻烦,因为一旦模型修改,你需要重复上面的步骤,至少要修改xaml的代码。
1472 0
|
C#
WPF:将Office文档、任意类型文件嵌入到EXE可执行文件中
原文:WPF:将Office文档、任意类型文件嵌入到EXE可执行文件中 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/article/details/50259527 RT 可能这个需求比较奇怪 做了一个绿色版应用,最后只生成一个可执行的exe文件,不附带任何dll和其他文件。
1439 0
|
安全 C# 定位技术
WPF:WebBrowser提示 为帮助保护你的安全,您的Web浏览器已经限制此文件显示可能访问您的计算机的活动内容
原文:WPF:WebBrowser提示 为帮助保护你的安全,您的Web浏览器已经限制此文件显示可能访问您的计算机的活动内容 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.
2902 0