-
Notifications
You must be signed in to change notification settings - Fork 7
/
FolderChooser2.cs
218 lines (198 loc) · 8.82 KB
/
FolderChooser2.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Provides a user-friendly folder chooser for VBSxript
// adapted from https://stackoverflow.com/questions/15368771/show-detailed-folder-browser-from-a-propertygrid#15386992
// by Simon Mourier https://stackoverflow.com/users/403671/simon-mourier
using System;
using System.Runtime.InteropServices;
using System.IO;
namespace VBScripting
{
/// <summary> Present the Windows Vista-style open file dialog to select a folder. </summary>
/// <remarks> Adapted from <a title="stackoverflow.com" href="https://stackoverflow.com/questions/15368771/show-detailed-folder-browser-from-a-propertygrid#15386992"> a stackoverflow post</a> by <a title="stackoverflow.com" href="https://stackoverflow.com/users/403671/simon-mourier"> Simon Mourier</a>. Uses <code> System.Runtime.InteropServices</code>. </remarks>
[ProgId( "VBScripting.FolderChooser2" ),
ClassInterface(ClassInterfaceType.None),
Guid("2650C2AB-B2F8-495F-AB4D-6C61BD463EA4")]
public class FolderChooser2 : IFolderChooser2
{
private string _initialDirectory;
private string _folderName;
private string _title;
/// <summary> Gets or sets the initial directory that the folder select dialog opens to. </summary>
/// <remarks> Environment variables are allowed. Relative paths are allowed. Optional. The default value is the current directory. </remarks>
public string InitialDirectory
{
get { return string.IsNullOrEmpty(_initialDirectory) ? Environment.CurrentDirectory : _initialDirectory; }
set { _initialDirectory = value; }
}
/// <summary> Sets the title/caption of the folder select dialog. Optional. The default value is "Select a folder". </summary>
public string Title
{
get
{
return string.IsNullOrEmpty(_title)
? "Select a folder"
: _title;
}
set { _title = value; }
}
/// <summary> Opens a dialog and returns the folder selected by the user. Returns an empty string if the user cancels. </summary>
/// <returns> a path </returns>
public string FolderName
{
get { return this.ShowDialog() ? _folderName : string.Empty; }
private set { _folderName = value; }
}
private bool ShowDialog()
{
return ShowDialog(IntPtr.Zero);
}
private bool ShowDialog(IntPtr hwndOwner)
{
IFileOpenDialog dialog = (IFileOpenDialog)new FileOpenDialog();
try
{
IShellItem item;
if (!string.IsNullOrEmpty(InitialDirectory))
{
var dir1 = InitialDirectory;
dir1 = Environment.ExpandEnvironmentVariables(dir1); // expand environment variables
dir1 = Path.GetFullPath(dir1); // resolve relative path
IntPtr idl;
uint atts = 0;
if (SHILCreateFromPath(dir1, out idl, ref atts) == 0)
{
if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0)
{
dialog.SetFolder(item);
}
}
}
dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM);
dialog.SetTitle(Title);
uint hr = dialog.Show(hwndOwner);
if (hr == ERROR_CANCELLED)
{
_folderName = string.Empty;
return false;
}
if (hr != 0)
{
_folderName = string.Empty;
return false;
}
dialog.GetResult(out item);
string path;
item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path);
_folderName = path;
return true;
}
finally
{
Marshal.ReleaseComObject(dialog);
}
}
[DllImport( "shell32.dll" )]
private static extern int SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, out IntPtr ppIdl, ref uint rgflnOut);
[DllImport( "shell32.dll" )]
private static extern int SHCreateShellItem(IntPtr pidlParent, IntPtr psfParent, IntPtr pidl, out IShellItem ppsi);
[DllImport( "user32.dll" )]
private static extern IntPtr GetActiveWindow();
private const uint ERROR_CANCELLED = 0x800704C7;
[ComImport]
[Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
private class FileOpenDialog
{
}
[ComImport]
[Guid("42f85136-db7e-439c-85f1-e4075d135fc8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IFileOpenDialog
{
[PreserveSig]
uint Show([In] IntPtr parent); // IModalWindow
void SetFileTypes(); // not fully defined
void SetFileTypeIndex([In] uint iFileType);
void GetFileTypeIndex(out uint piFileType);
void Advise(); // not fully defined
void Unadvise();
void SetOptions([In] FOS fos);
void GetOptions(out FOS pfos);
void SetDefaultFolder(IShellItem psi);
void SetFolder(IShellItem psi);
void GetFolder(out IShellItem ppsi);
void GetCurrentSelection(out IShellItem ppsi);
void SetFileName([In, MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
void SetTitle([In, MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
void SetOkButtonLabel([In, MarshalAs(UnmanagedType.LPWStr)] string pszText);
void SetFileNameLabel([In, MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
void GetResult(out IShellItem ppsi);
void AddPlace(IShellItem psi, int alignment);
void SetDefaultExtension([In, MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
void Close(int hr);
void SetClientGuid(); // not fully defined
void ClearClientData();
void SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
void GetResults([MarshalAs(UnmanagedType.Interface)] out IntPtr ppenum); // not fully defined
void GetSelectedItems([MarshalAs(UnmanagedType.Interface)] out IntPtr ppsai); // not fully defined
}
[ComImport]
[Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IShellItem
{
void BindToHandler(); // not fully defined
void GetParent(); // not fully defined
void GetDisplayName([In] SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
void GetAttributes(); // not fully defined
void Compare(); // not fully defined
}
private enum SIGDN : uint
{
SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000,
SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
SIGDN_FILESYSPATH = 0x80058000,
SIGDN_NORMALDISPLAY = 0,
SIGDN_PARENTRELATIVE = 0x80080001,
SIGDN_PARENTRELATIVEEDITING = 0x80031001,
SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001,
SIGDN_PARENTRELATIVEPARSING = 0x80018001,
SIGDN_URL = 0x80068000
}
[Flags]
private enum FOS
{
FOS_ALLNONSTORAGEITEMS = 0x80,
FOS_ALLOWMULTISELECT = 0x200,
FOS_CREATEPROMPT = 0x2000,
FOS_DEFAULTNOMINIMODE = 0x20000000,
FOS_DONTADDTORECENT = 0x2000000,
FOS_FILEMUSTEXIST = 0x1000,
FOS_FORCEFILESYSTEM = 0x40,
FOS_FORCESHOWHIDDEN = 0x10000000,
FOS_HIDEMRUPLACES = 0x20000,
FOS_HIDEPINNEDPLACES = 0x40000,
FOS_NOCHANGEDIR = 8,
FOS_NODEREFERENCELINKS = 0x100000,
FOS_NOREADONLYRETURN = 0x8000,
FOS_NOTESTFILECREATE = 0x10000,
FOS_NOVALIDATE = 0x100,
FOS_OVERWRITEPROMPT = 2,
FOS_PATHMUSTEXIST = 0x800,
FOS_PICKFOLDERS = 0x20,
FOS_SHAREAWARE = 0x4000,
FOS_STRICTFILETYPES = 4
}
}
/// <summary> COM interface for VBScripting.FolderChooser2 </summary>
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
Guid("2650C2AB-B3F8-495F-AB4D-6C61BD463EA4")]
public interface IFolderChooser2
{
/// <summary> </summary>
string InitialDirectory { get; set; }
/// <summary> </summary>
string Title { get; set; }
/// <summary> </summary>
string FolderName { get; }
}
}