C++获得系统路径

源代码:

#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;
}

在XP上,用VS2010编译通过,如果是win7或者更高版本,应该用

HRESULT SHGetKnownFolderPath(
  _In_      REFKNOWNFOLDERID rfid,
  _In_      DWORD dwFlags,
  _In_opt_  HANDLE hToken,
  _Out_     PWSTR *ppszPath
);

结果:

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++获得系统路径)