System.Environment.GetFolderPath函数可以通过Environment.SpecialFolder枚举参数获取系统特定的目录,比如桌面,我的文档,收藏夹等..但是有部分参数.NetFrameWork2.0并未开放,比如AllUser用户的这些特殊目录。我们可以通过windowsApi函数SHGetFolderPath来获取,如果你能通过Reflector看一下FrameWork代码就明白了。
代码如下:
using
System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
... {
class Program
... {
[DllImport( " shfolder.dll " , CharSet = CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
static void Main( string [] args)
... {
for ( int i = 0 ; i <= 255 ; i ++ )
... {
StringBuilder lpszPath = new StringBuilder( 260 );
SHGetFolderPath(IntPtr.Zero, i, IntPtr.Zero, 0 , lpszPath);
Console.WriteLine(i.ToString() + " : " + lpszPath);
}
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
... {
class Program
... {
[DllImport( " shfolder.dll " , CharSet = CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
static void Main( string [] args)
... {
for ( int i = 0 ; i <= 255 ; i ++ )
... {
StringBuilder lpszPath = new StringBuilder( 260 );
SHGetFolderPath(IntPtr.Zero, i, IntPtr.Zero, 0 , lpszPath);
Console.WriteLine(i.ToString() + " : " + lpszPath);
}
Console.Read();
}
}
}
结果如下
0
:C:/Documents and Settings/Administrator/桌面
1 :
2 :C:/Documents and Settings/Administrator/「开始」菜单/程序
3 :
4 :
5 :C:/Documents and Settings/Administrator/My Documents
6 :C:/Documents and Settings/Administrator/Favorites
7 :C:/Documents and Settings/Administrator/「开始」菜单/程序/启动
8 :C:/Documents and Settings/Administrator/Recent
9 :C:/Documents and Settings/Administrator/SendTo
10 :
11 :C:/Documents and Settings/Administrator/「开始」菜单
12 :
13 :C:/Documents and Settings/Administrator/My Documents/My Music
14 :C:/Documents and Settings/Administrator/My Documents/My Videos
15 :
16 :C:/Documents and Settings/Administrator/桌面
17 :
18 :
19 :C:/Documents and Settings/Administrator/NetHood
20 :C:/WINDOWS/Fonts
21 :C:/Documents and Settings/Administrator/Templates
22 :C:/Documents and Settings/All Users/「开始」菜单
23 :C:/Documents and Settings/All Users/「开始」菜单/程序
24 :C:/Documents and Settings/All Users/「开始」菜单/程序/启动
25 :C:/Documents and Settings/All Users/桌面
26 :C:/Documents and Settings/Administrator/Application Data
27 :C:/Documents and Settings/Administrator/PrintHood
28 :C:/Documents and Settings/Administrator/Local Settings/Application Data
29 :
30 :
31 :C:/Documents and Settings/All Users/Favorites
32 :C:/Documents and Settings/Administrator/Local Settings/Temporary Internet Files
33 :C:/Documents and Settings/Administrator/Cookies
34 :C:/Documents and Settings/Administrator/Local Settings/History
35 :C:/Documents and Settings/All Users/Application Data
36 :C:/WINDOWS
37 :C:/WINDOWS/system32
38 :C:/Program Files
39 :C:/Documents and Settings/Administrator/My Documents/My Pictures
40 :C:/Documents and Settings/Administrator
41 :C:/WINDOWS/system32
42 :
43 :C:/Program Files/Common Files
44 :
45 :C:/Documents and Settings/All Users/Templates
46 :C:/Documents and Settings/All Users/Documents
47 :C:/Documents and Settings/All Users/「开始」菜单/程序/管理工具
48 :C:/Documents and Settings/Administrator/「开始」菜单/程序/管理工具
49 :
50 :
51 :
52 :
53 :C:/Documents and Settings/All Users/Documents/My Music
54 :
55 :C:/Documents and Settings/All Users/Documents/My Videos
56 :C:/WINDOWS/resources
57 :
58 :
59 :C:/Documents and Settings/Administrator/Local Settings/Application Data/Microsoft/CD Burning
1 :
2 :C:/Documents and Settings/Administrator/「开始」菜单/程序
3 :
4 :
5 :C:/Documents and Settings/Administrator/My Documents
6 :C:/Documents and Settings/Administrator/Favorites
7 :C:/Documents and Settings/Administrator/「开始」菜单/程序/启动
8 :C:/Documents and Settings/Administrator/Recent
9 :C:/Documents and Settings/Administrator/SendTo
10 :
11 :C:/Documents and Settings/Administrator/「开始」菜单
12 :
13 :C:/Documents and Settings/Administrator/My Documents/My Music
14 :C:/Documents and Settings/Administrator/My Documents/My Videos
15 :
16 :C:/Documents and Settings/Administrator/桌面
17 :
18 :
19 :C:/Documents and Settings/Administrator/NetHood
20 :C:/WINDOWS/Fonts
21 :C:/Documents and Settings/Administrator/Templates
22 :C:/Documents and Settings/All Users/「开始」菜单
23 :C:/Documents and Settings/All Users/「开始」菜单/程序
24 :C:/Documents and Settings/All Users/「开始」菜单/程序/启动
25 :C:/Documents and Settings/All Users/桌面
26 :C:/Documents and Settings/Administrator/Application Data
27 :C:/Documents and Settings/Administrator/PrintHood
28 :C:/Documents and Settings/Administrator/Local Settings/Application Data
29 :
30 :
31 :C:/Documents and Settings/All Users/Favorites
32 :C:/Documents and Settings/Administrator/Local Settings/Temporary Internet Files
33 :C:/Documents and Settings/Administrator/Cookies
34 :C:/Documents and Settings/Administrator/Local Settings/History
35 :C:/Documents and Settings/All Users/Application Data
36 :C:/WINDOWS
37 :C:/WINDOWS/system32
38 :C:/Program Files
39 :C:/Documents and Settings/Administrator/My Documents/My Pictures
40 :C:/Documents and Settings/Administrator
41 :C:/WINDOWS/system32
42 :
43 :C:/Program Files/Common Files
44 :
45 :C:/Documents and Settings/All Users/Templates
46 :C:/Documents and Settings/All Users/Documents
47 :C:/Documents and Settings/All Users/「开始」菜单/程序/管理工具
48 :C:/Documents and Settings/Administrator/「开始」菜单/程序/管理工具
49 :
50 :
51 :
52 :
53 :C:/Documents and Settings/All Users/Documents/My Music
54 :
55 :C:/Documents and Settings/All Users/Documents/My Videos
56 :C:/WINDOWS/resources
57 :
58 :
59 :C:/Documents and Settings/Administrator/Local Settings/Application Data/Microsoft/CD Burning
具体意义可以参照Environment.SpecialFolder枚举说明和windows基本常识来理解。