-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFolderSelectDialog.cs
154 lines (132 loc) · 3.68 KB
/
FolderSelectDialog.cs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Windows.Forms;
// ------------------------------------------------------------------
// Wraps System.Windows.Forms.OpenFileDialog to make it present
// a vista-style dialog.
// ------------------------------------------------------------------
namespace FolderSelect
{
/// <summary>
/// Wraps System.Windows.Forms.OpenFileDialog to make it present
/// a vista-style dialog.
/// </summary>
public class FolderSelectDialog
{
// Wrapped dialog
System.Windows.Forms.OpenFileDialog ofd = null;
/// <summary>
/// Default constructor
/// </summary>
public FolderSelectDialog()
{
ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "Folders|\n";
ofd.AddExtension = false;
ofd.CheckFileExists = false;
ofd.DereferenceLinks = true;
ofd.Multiselect = false;
}
#region Properties
/// <summary>
/// Gets/Sets the initial folder to be selected. A null value selects the current directory.
/// </summary>
public string InitialDirectory
{
get { return ofd.InitialDirectory; }
set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
}
/// <summary>
/// Gets/Sets the title to show in the dialog
/// </summary>
public string Title
{
get { return ofd.Title; }
set { ofd.Title = value == null ? "Select a folder" : value; }
}
/// <summary>
/// Gets the selected folder
/// </summary>
public string FileName
{
get { return ofd.FileName; }
}
#endregion
#region Methods
/// <summary>
/// Shows the dialog
/// </summary>
/// <returns>True if the user presses OK else false</returns>
public bool ShowDialog()
{
return ShowDialog(IntPtr.Zero);
}
/// <summary>
/// Shows the dialog
/// </summary>
/// <param name="hWndOwner">Handle of the control to be parent</param>
/// <returns>True if the user presses OK else false</returns>
public bool ShowDialog(IntPtr hWndOwner)
{
bool flag = false;
if (Environment.OSVersion.Version.Major >= 6)
{
var r = new Reflector("System.Windows.Forms");
uint num = 0;
Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
object dialog = r.Call(ofd, "CreateVistaDialog");
r.Call(ofd, "OnBeforeVistaDialog", dialog);
uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
object[] parameters = new object[] { pfde, num };
r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
num = (uint)parameters[1];
try
{
int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
flag = 0 == num2;
}
finally
{
r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
GC.KeepAlive(pfde);
}
}
else
{
var fbd = new FolderBrowserDialog();
fbd.Description = this.Title;
fbd.SelectedPath = this.InitialDirectory;
fbd.ShowNewFolderButton = false;
if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
ofd.FileName = fbd.SelectedPath;
flag = true;
}
return flag;
}
#endregion
}
/// <summary>
/// Creates IWin32Window around an IntPtr
/// </summary>
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="handle">Handle to wrap</param>
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
/// <summary>
/// Original ptr
/// </summary>
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
}