1
+ using System ;
2
+ using System . Runtime . InteropServices ;
3
+
4
+ /// <summary>
5
+ /// Class containing methods to retrieve specific file system paths.
6
+ /// </summary>
7
+ public static class KnownFolders
8
+ {
9
+ private static string [ ] _knownFolderGuids = new string [ ]
10
+ {
11
+ "{56784854-C6CB-462B-8169-88E350ACB882}" , // Contacts
12
+ "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}" , // Desktop
13
+ "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}" , // Documents
14
+ "{374DE290-123F-4565-9164-39C4925E467B}" , // Downloads
15
+ "{1777F761-68AD-4D8A-87BD-30B759FA33DD}" , // Favorites
16
+ "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}" , // Links
17
+ "{4BD8D571-6D19-48D3-BE97-422220080E43}" , // Music
18
+ "{33E28130-4E1E-4676-835A-98395C3BC3BB}" , // Pictures
19
+ "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}" , // SavedGames
20
+ "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}" , // SavedSearches
21
+ "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}" , // Videos
22
+ } ;
23
+
24
+ /// <summary>
25
+ /// Gets the current path to the specified known folder as currently configured. This does
26
+ /// not require the folder to be existent.
27
+ /// </summary>
28
+ /// <param name="knownFolder">The known folder which current path will be returned.</param>
29
+ /// <returns>The default path of the known folder.</returns>
30
+ /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
31
+ /// could not be retrieved.</exception>
32
+ public static string GetPath ( KnownFolder knownFolder )
33
+ {
34
+ return GetPath ( knownFolder , false ) ;
35
+ }
36
+
37
+ /// <summary>
38
+ /// Gets the current path to the specified known folder as currently configured. This does
39
+ /// not require the folder to be existent.
40
+ /// </summary>
41
+ /// <param name="knownFolder">The known folder which current path will be returned.</param>
42
+ /// <param name="defaultUser">Specifies if the paths of the default user (user profile
43
+ /// template) will be used. This requires administrative rights.</param>
44
+ /// <returns>The default path of the known folder.</returns>
45
+ /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path
46
+ /// could not be retrieved.</exception>
47
+ public static string GetPath ( KnownFolder knownFolder , bool defaultUser )
48
+ {
49
+ return GetPath ( knownFolder , KnownFolderFlags . DontVerify , defaultUser ) ;
50
+ }
51
+
52
+ private static string GetPath ( KnownFolder knownFolder , KnownFolderFlags flags ,
53
+ bool defaultUser )
54
+ {
55
+ int result = SHGetKnownFolderPath ( new Guid ( _knownFolderGuids [ ( int ) knownFolder ] ) ,
56
+ ( uint ) flags , new IntPtr ( defaultUser ? - 1 : 0 ) , out IntPtr outPath ) ;
57
+ if ( result >= 0 )
58
+ {
59
+ string path = Marshal . PtrToStringUni ( outPath ) ;
60
+ Marshal . FreeCoTaskMem ( outPath ) ;
61
+ return path ;
62
+ }
63
+ else
64
+ {
65
+ //throw new ExternalException("Unable to retrieve the known folder path. It may not be available on this system.", result);
66
+ return null ;
67
+ }
68
+ }
69
+
70
+ [ DllImport ( "Shell32.dll" ) ]
71
+ private static extern int SHGetKnownFolderPath (
72
+ [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid rfid , uint dwFlags , IntPtr hToken ,
73
+ out IntPtr ppszPath ) ;
74
+
75
+ [ Flags ]
76
+ private enum KnownFolderFlags : uint
77
+ {
78
+ SimpleIDList = 0x00000100 ,
79
+ NotParentRelative = 0x00000200 ,
80
+ DefaultPath = 0x00000400 ,
81
+ Init = 0x00000800 ,
82
+ NoAlias = 0x00001000 ,
83
+ DontUnexpand = 0x00002000 ,
84
+ DontVerify = 0x00004000 ,
85
+ Create = 0x00008000 ,
86
+ NoAppcontainerRedirection = 0x00010000 ,
87
+ AliasOnly = 0x80000000
88
+ }
89
+ }
90
+
91
+ /// <summary>
92
+ /// Standard folders registered with the system. These folders are installed with Windows Vista
93
+ /// and later operating systems, and a computer will have only folders appropriate to it
94
+ /// installed.
95
+ /// </summary>
96
+ public enum KnownFolder
97
+ {
98
+ Contacts ,
99
+ Desktop ,
100
+ Documents ,
101
+ Downloads ,
102
+ Favorites ,
103
+ Links ,
104
+ Music ,
105
+ Pictures ,
106
+ SavedGames ,
107
+ SavedSearches ,
108
+ Videos
109
+ }
0 commit comments