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; 所以暫時選擇第二種.