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 174 175 176 177 178 179 180 181 182 183 184
| using System.Reflection; using System.Windows.Forms; using System; public class FolderSelectDialog { private OpenFileDialog openFileDialog = null;
public FolderSelectDialog() { openFileDialog = new OpenFileDialog { Filter = "Folders|\n", AddExtension = false, CheckFileExists = false, DereferenceLinks = true, Multiselect = false }; }
#region Properties public string InitialDirectory { get { return openFileDialog.InitialDirectory; } set { openFileDialog.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; } }
public string Title { get { return openFileDialog.Title; } set { openFileDialog.Title = value ?? "Select a folder"; } }
public string FolderName { get { return openFileDialog.FileName; } }
#endregion
#region Methods public bool ShowDialog() { return ShowDialog(IntPtr.Zero); }
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(openFileDialog, "CreateVistaDialog"); r.Call(openFileDialog, "OnBeforeVistaDialog", dialog);
uint options = (uint)r.CallAs(typeof(FileDialog), openFileDialog, "GetOptions"); options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS"); r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
object pfde = r.New("FileDialog.VistaDialogEvents", openFileDialog); 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 folderBrowserDialog = new FolderBrowserDialog { Description = this.Title, SelectedPath = this.InitialDirectory, ShowNewFolderButton = false }; if (folderBrowserDialog.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false; openFileDialog.FileName = folderBrowserDialog.SelectedPath; flag = true; }
return flag; }
#endregion }
public class WindowWrapper : IWin32Window { private IntPtr _hwnd; public WindowWrapper(IntPtr handle) { _hwnd = handle; } public IntPtr Handle { get { return _hwnd; } } }
public class Reflector { #region variables
string m_ns; public Assembly assembly { get; private set; }
#endregion
#region Constructors public Reflector(string ns) : this(ns, ns) { } public Reflector(string assemblyName, string ns) { m_ns = ns; assembly = null; foreach (AssemblyName name in Assembly.GetExecutingAssembly().GetReferencedAssemblies()) { if (name.FullName.StartsWith(assemblyName)) { assembly = Assembly.Load(name); break; } } }
public Reflector(Assembly assembly) { this.assembly = assembly; }
#endregion
#region Methods public Type GetType(string typeName) { Type type = null; string[] names = typeName.Split('.');
if (names.Length > 0) type = assembly.GetType(m_ns + "." + names[0]);
for (int i = 1; i < names.Length; ++i) { type = type.GetNestedType(names[i], BindingFlags.NonPublic); } return type; } public object New(string name, params object[] parameters) { Type type = GetType(name);
ConstructorInfo[] ctorInfos = type.GetConstructors(); foreach (ConstructorInfo ci in ctorInfos) { try { return ci.Invoke(parameters); } catch { } }
return null; }
public object Call(object obj, string func, params object[] parameters) => Call2(obj, func, parameters); public object Call2(object obj, string func, object[] parameters) => CallAs2(obj.GetType(), obj, func, parameters); public object CallAs(Type type, object obj, string func, params object[] parameters) => CallAs2(type, obj, func, parameters);
public object CallAs2(Type type, object obj, string func, object[] parameters) { MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return methInfo.Invoke(obj, parameters); }
public object Get(object obj, string prop) => GetAs(obj.GetType(), obj, prop);
public object GetAs(Type type, object obj, string prop) { PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return propInfo.GetValue(obj, null); }
public object GetEnum(string typeName, string name) { Type type = GetType(typeName); FieldInfo fieldInfo = type.GetField(name); return fieldInfo.GetValue(null); } #endregion }
|