红炉点雪

C++获取系统文件夹路径

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
#include <Shlobj.h>
#include <stdio.h>
#include <locale.h>

int main()
{
setlocale(LC_ALL, "chs");

TCHAR szPath[MAX_PATH];

int CSIDL[] = {
CSIDL_ADMINTOOLS,
CSIDL_APPDATA,
CSIDL_CDBURN_AREA,
CSIDL_COMMON_ADMINTOOLS,
CSIDL_COMMON_APPDATA,
CSIDL_COMMON_DESKTOPDIRECTORY,
CSIDL_COMMON_DOCUMENTS,
CSIDL_COMMON_FAVORITES,
CSIDL_COMMON_MUSIC,
CSIDL_COMMON_PICTURES,
CSIDL_COMMON_PROGRAMS,
CSIDL_COMMON_STARTMENU,
CSIDL_COMMON_STARTUP,
CSIDL_COMMON_TEMPLATES,
CSIDL_COMMON_VIDEO,
CSIDL_COOKIES,
CSIDL_FAVORITES,
CSIDL_HISTORY,
CSIDL_INTERNET_CACHE,
CSIDL_LOCAL_APPDATA,
CSIDL_MYDOCUMENTS,
CSIDL_MYMUSIC,
CSIDL_MYPICTURES,
CSIDL_MYVIDEO,
CSIDL_PROFILE,
CSIDL_PROGRAM_FILES,
CSIDL_PROGRAM_FILES_COMMON,
CSIDL_PROGRAMS,
CSIDL_RECENT,
CSIDL_SENDTO,
CSIDL_STARTMENU,
CSIDL_STARTUP,
CSIDL_SYSTEM,
CSIDL_TEMPLATES,
CSIDL_WINDOWS,
0
};

for (int i=0;CSIDL[i];i++) {
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL[i], NULL, SHGFP_TYPE_CURRENT, szPath))) {
wprintf(L"%d, %s\n", i, szPath);
}
}

return 0;
}

输出:

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
0, C:\Documents and Settings\Administrator\「开始」菜单\程序\管理工具
1, C:\Documents and Settings\Administrator\Application Data
2, C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\CD Burning
3, C:\Documents and Settings\All Users\「开始」菜单\程序\管理工具
4, C:\Documents and Settings\All Users\Application Data
5, C:\Documents and Settings\All Users\桌面
6, C:\Documents and Settings\All Users\Documents
7, C:\Documents and Settings\All Users\Favorites
8, C:\Documents and Settings\All Users\Documents\My Music
9, C:\Documents and Settings\All Users\Documents\My Pictures
10, C:\Documents and Settings\All Users\「开始」菜单\程序
11, C:\Documents and Settings\All Users\「开始」菜单
12, C:\Documents and Settings\All Users\「开始」菜单\程序\启动
13, C:\Documents and Settings\All Users\Templates
14, C:\Documents and Settings\All Users\Documents\My Videos
15, D:\My Documents\Cookies
16, D:\My Documents\Favorites
17, C:\Documents and Settings\Administrator\Local Settings\History
18, C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files
19, C:\Documents and Settings\Administrator\Local Settings\Application Data
20, D:\My Documents
21, D:\My Documents\My Music
22, D:\My Documents\My Pictures
23, D:\My Documents\My Videos
24, C:\Documents and Settings\Administrator
25, C:\Program Files
26, C:\Program Files\Common Files
27, C:\Documents and Settings\Administrator\「开始」菜单\程序
28, C:\Documents and Settings\Administrator\Recent
29, C:\Documents and Settings\Administrator\SendTo
30, C:\Documents and Settings\Administrator\「开始」菜单
31, C:\Documents and Settings\Administrator\「开始」菜单\程序\启动
32, C:\WINDOWS\system32
33, C:\Documents and Settings\Administrator\Templates
34, C:\WINDOWS

相关参考:

C++获得系统路径-feier7501
系统文件夹路径的系统宏定义及意义,使用SHGetFolderPath API获得路径示例-周强

CONTACT