-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirHelpers.cs
231 lines (210 loc) · 9.17 KB
/
DirHelpers.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
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Runtime.InteropServices;
/// <summary>
/// Class containing methods to retrieve specific file system paths.
/// </summary>
public static class KnownFolders
{
private static readonly string[] _knownFolderGuids = new string[]
{
"{56784854-C6CB-462B-8169-88E350ACB882}", // Contacts
"{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}", // Desktop
"{FDD39AD0-238F-46AF-ADB4-6C85480369C7}", // Documents
"{374DE290-123F-4565-9164-39C4925E467B}", // Downloads
"{1777F761-68AD-4D8A-87BD-30B759FA33DD}", // Favorites
"{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}", // Links
"{4BD8D571-6D19-48D3-BE97-422220080E43}", // Music
"{33E28130-4E1E-4676-835A-98395C3BC3BB}", // Pictures
"{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}", // SavedGames
"{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}", // SavedSearches
"{18989B1D-99B5-455B-841C-AB7C74E4DDFC}", // Videos
};
/// <summary>
/// Gets the current path to the specified known folder as currently configured. This does
/// not require the folder to be existent.
/// </summary>
/// <param name="knownFolder">The known folder which current path will be returned.</param>
/// <returns>The default path of the known folder.</returns>
/// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
/// could not be retrieved.</exception>
public static string GetPath(KnownFolder knownFolder)
{
return GetPath(knownFolder, false);
}
/// <summary>
/// Gets the current path to the specified known folder as currently configured. This does
/// not require the folder to be existent.
/// </summary>
/// <param name="knownFolder">The known folder which current path will be returned.</param>
/// <param name="defaultUser">Specifies if the paths of the default user (user profile
/// template) will be used. This requires administrative rights.</param>
/// <returns>The default path of the known folder.</returns>
/// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
/// could not be retrieved.</exception>
public static string GetPath(KnownFolder knownFolder, bool defaultUser)
{
return GetPath(knownFolder, KnownFolderFlags.DontVerify, defaultUser);
}
/// <summary>
/// Gets the default path to the specified known folder. This does not require the folder
/// to be existent.
/// </summary>
/// <param name="knownFolder">The known folder which default path will be returned.</param>
/// <returns>The current (and possibly redirected) path of the known folder.</returns>
/// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
/// could not be retrieved.</exception>
public static string GetDefaultPath(KnownFolder knownFolder)
{
return GetDefaultPath(knownFolder, false);
}
/// <summary>
/// Gets the default path to the specified known folder. This does not require the folder
/// to be existent.
/// </summary>
/// <param name="knownFolder">The known folder which default path will be returned.</param>
/// <param name="defaultUser">Specifies if the paths of the default user (user profile
/// template) will be used. This requires administrative rights.</param>
/// <returns>The current (and possibly redirected) path of the known folder.</returns>
/// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
/// could not be retrieved.</exception>
public static string GetDefaultPath(KnownFolder knownFolder, bool defaultUser)
{
return GetPath(knownFolder, KnownFolderFlags.DefaultPath | KnownFolderFlags.DontVerify,
defaultUser);
}
/// <summary>
/// Creates and initializes the known folder.
/// </summary>
/// <param name="knownFolder">The known folder which will be initialized.</param>
/// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the known
/// folder could not be initialized.</exception>
public static void Initialize(KnownFolder knownFolder)
{
Initialize(knownFolder, false);
}
/// <summary>
/// Creates and initializes the known folder.
/// </summary>
/// <param name="knownFolder">The known folder which will be initialized.</param>
/// <param name="defaultUser">Specifies if the paths of the default user (user profile
/// template) will be used. This requires administrative rights.</param>
/// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the known
/// folder could not be initialized.</exception>
public static void Initialize(KnownFolder knownFolder, bool defaultUser)
{
GetPath(knownFolder, KnownFolderFlags.Create | KnownFolderFlags.Init, defaultUser);
}
private static string GetPath(KnownFolder knownFolder, KnownFolderFlags flags,
bool defaultUser)
{
IntPtr outPath;
int result = SHGetKnownFolderPath(new Guid(_knownFolderGuids[(int)knownFolder]),
(uint)flags, new IntPtr(defaultUser ? -1 : 0), out outPath);
if (result >= 0)
{
return Marshal.PtrToStringUni(outPath);
}
else
{
throw new ExternalException("Unable to retrieve the known folder path. It may not "
+ "be available on this system.", result);
}
}
/// <summary>
/// Retrieves the full path of a known folder identified by the folder's KnownFolderID.
/// </summary>
/// <param name="rfid">A KnownFolderID that identifies the folder.</param>
/// <param name="dwFlags">Flags that specify special retrieval options. This value can be
/// 0; otherwise, one or more of the KnownFolderFlag values.</param>
/// <param name="hToken">An access token that represents a particular user. If this
/// parameter is NULL, which is the most common usage, the function requests the known
/// folder for the current user. Assigning a value of -1 indicates the Default User.
/// The default user profile is duplicated when any new user account is created.
/// Note that access to the Default User folders requires administrator privileges.
/// </param>
/// <param name="ppszPath">When this method returns, contains the address of a string that
/// specifies the path of the known folder. The returned path does not include a
/// trailing backslash.</param>
/// <returns>Returns S_OK if successful, or an error value otherwise.</returns>
[DllImport("Shell32.dll")]
private static extern int SHGetKnownFolderPath(
[MarshalAs(UnmanagedType.LPStruct)]Guid rfid, uint dwFlags, IntPtr hToken,
out IntPtr ppszPath);
[Flags]
private enum KnownFolderFlags : uint
{
SimpleIDList = 0x00000100,
NotParentRelative = 0x00000200,
DefaultPath = 0x00000400,
Init = 0x00000800,
NoAlias = 0x00001000,
DontUnexpand = 0x00002000,
DontVerify = 0x00004000,
Create = 0x00008000,
NoAppcontainerRedirection = 0x00010000,
AliasOnly = 0x80000000
}
}
/// <summary>
/// Standard folders registered with the system. These folders are installed with Windows Vista
/// and later operating systems, and a computer will have only folders appropriate to it
/// installed.
/// </summary>
public enum KnownFolder
{
/// <summary>
/// The per-user Contacts folder. Introduced in Windows Vista.
/// Defaults to "%USERPROFILE%\Contacts".
/// </summary>
Contacts,
/// <summary>
/// The per-user Desktop folder.
/// Defaults to "%USERPROFILE%\Desktop".
/// </summary>
Desktop,
/// <summary>
/// The per-user Documents folder.
/// Defaults to "%USERPROFILE%\Documents".
/// </summary>
Documents,
/// <summary>
/// The per-user Downloads folder.
/// Defaults to "%USERPROFILE%\Downloads".
/// </summary>
Downloads,
/// <summary>
/// The per-user Favorites folder.
/// Defaults to "%USERPROFILE%\Favorites".
/// </summary>
Favorites,
/// <summary>
/// The per-user Links folder.
/// Defaults to "%USERPROFILE%\Links".
/// </summary>
Links,
/// <summary>
/// The per-user Music folder.
/// Defaults to "%USERPROFILE%\Music".
/// </summary>
Music,
/// <summary>
/// The per-user Pictures folder.
/// Defaults to "%USERPROFILE%\Pictures".
/// </summary>
Pictures,
/// <summary>
/// The per-user Saved Games folder. Introduced in Windows Vista.
/// Defaults to "%USERPROFILE%\Saved Games".
/// </summary>
SavedGames,
/// <summary>
/// The per-user Searches folder.
/// Defaults to "%USERPROFILE%\Searches".
/// </summary>
SavedSearches,
/// <summary>
/// The per-user Videos folder.
/// Defaults to "%USERPROFILE%\Videos".
/// </summary>
Videos
}