-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFolderSelectDialog.cs
173 lines (143 loc) · 4.19 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Windows.Forms;
using System.Reflection;
namespace Grepy2
{
// based on https://www.lyquidity.com/devblog/?p=136
class FolderSelectDialog
{
OpenFileDialog OpenFileDir = null;
string name_string;
Assembly assembly;
public FolderSelectDialog()
{
OpenFileDir = new OpenFileDialog();
OpenFileDir.Filter = "Folders|\n";
OpenFileDir.Multiselect = false;
OpenFileDir.AddExtension = false;
OpenFileDir.CheckFileExists = false;
OpenFileDir.DereferenceLinks = true;
}
public string InitialDirectory
{
get { return OpenFileDir.InitialDirectory; }
set
{
bool bIsNull = ((value == null) || (value.Length == 0));
OpenFileDir.InitialDirectory = bIsNull ? Environment.CurrentDirectory : value;
}
}
public string Title
{
get { return OpenFileDir.Title; }
set
{
OpenFileDir.Title = (value == null) ? "Select a folder" : value;
}
}
public string FileName
{
get { return OpenFileDir.FileName; }
}
public bool ShowDialog()
{
return ShowDialog(IntPtr.Zero);
}
public bool ShowDialog(IntPtr hWndOwner)
{
if( Environment.OSVersion.Version.Major >= 6 ) // Vista/Win7/Win8/Win10
{
name_string = "System.Windows.Forms";
assembly = null;
AssemblyName[] referencedAssemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (AssemblyName assemblyName in referencedAssemblies)
{
if (assemblyName.FullName.StartsWith(name_string))
{
assembly = Assembly.Load(assemblyName);
break;
}
}
Type typeIFileDialog = GetType("FileDialogNative.IFileDialog");
object dialog = Call(OpenFileDir.GetType(), OpenFileDir, "CreateVistaDialog");
Call(OpenFileDir.GetType(), OpenFileDir, "OnBeforeVistaDialog", dialog);
uint options = (uint)Call(typeof(System.Windows.Forms.FileDialog), OpenFileDir, "GetOptions");
options = options | (uint)GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
Call(typeIFileDialog, dialog, "SetOptions", options);
object pFileDialogEvent = New("FileDialog.VistaDialogEvents", OpenFileDir);
uint num_parms = 0;
object[] parameters = new object[] { pFileDialogEvent, num_parms };
Call(typeIFileDialog, dialog, "Advise", parameters);
num_parms = (uint)parameters[1];
try
{
// show the dialog
int count = (int)Call(typeIFileDialog, dialog, "Show", hWndOwner);
return (count == 0);
}
finally
{
// remove event handler
Call(typeIFileDialog, dialog, "Unadvise", num_parms);
GC.KeepAlive(pFileDialogEvent);
}
}
else // XP and earlier
{
FolderBrowserDialog FolderBrowser = new FolderBrowserDialog();
FolderBrowser.Description = this.Title;
FolderBrowser.SelectedPath = this.InitialDirectory;
FolderBrowser.ShowNewFolderButton = false;
DialogResult dialog_result = FolderBrowser.ShowDialog();
if( dialog_result == DialogResult.OK )
{
OpenFileDir.FileName = FolderBrowser.SelectedPath;
return true;
}
}
return false;
}
public Type GetType(string typeName)
{
Type type = null;
string[] type_names = typeName.Split('.');
if (type_names.Length > 0)
{
type = assembly.GetType(name_string + "." + type_names[0]);
}
for (int i = 1; i < type_names.Length; ++i)
{
type = type.GetNestedType(type_names[i], BindingFlags.NonPublic);
}
return type;
}
public object GetEnum(string typeName, string name)
{
Type type = GetType(typeName);
FieldInfo fieldInfo = type.GetField(name);
return fieldInfo.GetValue(null);
}
public object Call(Type type, object obj, string func, params object[] parameters)
{
MethodInfo methodInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return methodInfo.Invoke(obj, parameters);
}
public object New(string name, params object[] parameters)
{
Type type = GetType(name);
ConstructorInfo[] constructorInfos = type.GetConstructors();
foreach (ConstructorInfo constructorInfo in constructorInfos)
{
try
{
return constructorInfo.Invoke(parameters);
}
catch
{
Console.WriteLine("constructorInfo.Invoke() failed");
}
}
return null;
}
}
}