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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
//
// System.Windows.Forms.FolderBrowserDialog.cs
//
// Author:
// Dennis Hayes (dennish@raytek.com)
// Implemented by Jordi Mas i Hernàndez (jmas@softcatala.org)
//
// (C) 2002-3 Ximian, Inc
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Runtime.InteropServices;
using System.Text;
namespace System.Windows.Forms {
// <summary>
//
// </summary>
using System.Runtime.Remoting;
using System.ComponentModel;
// Beta specs do not specify what class to defrive from.
// Using CommonDialog because
public class FolderBrowserDialog : CommonDialog {
string description;
string selectedPath;
Environment.SpecialFolder folder;
bool bShowNewFolderButton;
private IntPtr SpecialFolderConv(Environment.SpecialFolder fldr) {
IntPtr nRslt = IntPtr.Zero;
switch (fldr)
{
case Environment.SpecialFolder.ApplicationData:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_APPDATA;
break;
case Environment.SpecialFolder.CommonApplicationData:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_APPDATA;
break;
case Environment.SpecialFolder.CommonProgramFiles:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_PROGRAMS;
break;
case Environment.SpecialFolder.Cookies:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COOKIES;
break;
#if NET_1_1
case Environment.SpecialFolder.Desktop:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOP;
break;
//case Environment.SpecialFolder.MyComputer: //TODO: Which value?
//nRslt = (IntPtr) ShellSpecialFolder.;
//break;
#endif
case Environment.SpecialFolder.DesktopDirectory:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOPDIRECTORY;
break;
case Environment.SpecialFolder.Favorites:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_FAVORITES;
break;
case Environment.SpecialFolder.History:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_HISTORY;
break;
case Environment.SpecialFolder.InternetCache:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_INTERNET_CACHE;
break;
case Environment.SpecialFolder.LocalApplicationData:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_LOCAL_APPDATA;
break;
case Environment.SpecialFolder.MyMusic:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYMUSIC;
break;
case Environment.SpecialFolder.MyPictures:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYPICTURES;
break;
case Environment.SpecialFolder.Personal:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PERSONAL;
break;
case Environment.SpecialFolder.ProgramFiles:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAM_FILES;
break;
case Environment.SpecialFolder.Programs:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAMS;
break;
case Environment.SpecialFolder.Recent:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_RECENT;
break;
case Environment.SpecialFolder.SendTo:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SENDTO;
break;
case Environment.SpecialFolder.StartMenu:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTMENU;
break;
case Environment.SpecialFolder.Startup:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTUP;
break;
case Environment.SpecialFolder.System:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SYSTEM;
break;
case Environment.SpecialFolder.Templates:
nRslt = (IntPtr) ShellSpecialFolder.CSIDL_TEMPLATES;
break;
default:
throw new InvalidEnumArgumentException();
}
return nRslt;
}
//
// --- Constructor
//
public FolderBrowserDialog() {
Reset();
}
public override void Reset(){
description = "";
selectedPath = "";
#if NET_1_1
folder = Environment.SpecialFolder.Desktop;
#else
folder = Environment.SpecialFolder.DesktopDirectory;
#endif
bShowNewFolderButton = true;
}
protected override bool RunDialog(IntPtr hWndOwner){
BROWSEINFO bi = new BROWSEINFO();
IntPtr pidl = IntPtr.Zero;
IntPtr olePath = Marshal.AllocHGlobal(2048);
int nRet = 0;
bi.hwndOwner = hWndOwner;
bi.pidlRoot = (IntPtr) SpecialFolderConv(RootFolder);
bi.lpszTitle = Description;
bi.ulFlags = (uint) (BrowseDirFlags.BIF_RETURNONLYFSDIRS | BrowseDirFlags.BIF_STATUSTEXT);
bi.lpfn = IntPtr.Zero;
bi.lParam = IntPtr.Zero;
bi.iImage = 0;
bi.pszDisplayName = olePath;
pidl = Win32.SHBrowseForFolder(ref bi);
Marshal.FreeHGlobal(olePath);
if (pidl==IntPtr.Zero) return false;
StringBuilder sBuilder = new StringBuilder();
nRet = Win32.SHGetPathFromIDList(pidl, sBuilder);
// TODO: Dealocate the pidl returned by Win32.SHBrowseForFolder
// Win32.SHFreeMalloc(pidl);
if (nRet==0) return false;
selectedPath = sBuilder.ToString();
return true;
}
//
// --- Public Properties
//
public string Description {
get {return description;}
set {description = value;}
}
//beta docs do not have accessor.
//protected bool DesignMode {
//}
//protected EventHandlerList Events {
//}
public Environment.SpecialFolder RootFolder {
get {return folder;}
set {folder = value;}
}
public string SelectedPath {
get {return selectedPath;}
set {selectedPath = value;}
}
public bool ShowNewFolderButton {
get {return bShowNewFolderButton;}
set {bShowNewFolderButton = value;}
}
//public virtual System.ComponentModel.IContainer Container {
// get {
// throw new NotImplementedException ();
// }
//}
// FIXME: beta 1.1 says the following should be public virtual ISite Site {
// but the compiler gives warning that it must be new.
// Probably system.component needs to change to be beta 1.1 compliant
// looks fixed on 9/28/2003
public virtual ISite Site {
get {
throw new NotImplementedException ();
}
set {
//FIXME:
}
}
//Specs seem to say they need to be here, but adding them conflicts with commondialog : component.disposed/helprequest
//public event EventHandler Disposed;
//public event EventHandler HelpRequest;
}
}
|