使用API函数SHGetSpecialFolderLocation。shlobj.h里有SHGetSpecialFolderLocation的原型声明。这个函数可以帮我们找到Windows的桌面目录、启动目录、我的文档目录等。
SHGetSpecialFolder需要三个参数。 第一个参数是HWND,它指定了"所有者窗口":在调用这个函数时可能出现的对话框或消息框。第二个参数是一个整数id,决定哪个目录是待查找目录,它的取值可能是:
CSIDL_BITBUCKET 回收站
CSIDL_CONTROLS 控制面板
CSIDL_DESKTOP Windows 桌面Desktop
CSIDL_DESKTOPDIRECTORY Desktop的目录
CSIDL_DRIVES 我的电脑
CSIDL_FONTS 字体目录
CSIDL_NETHOOD 网上邻居
CSIDL_NETWORK 网上邻居虚拟目录
CSIDL_PERSONAL 我的文档
CSIDL_PRINTERS 打印机
CSIDL_PROGRAMS 程序组
CSIDL_RECENT 最近打开的文档
CSIDL_SENDTO “发送到”菜单项
CSIDL_STARTMENU 任务条启动菜单项
CSIDL_STARTUP 启动目录
CSIDL_TEMPLATES 文档模板
TCHAR szDocument[MAX_PATH]={0};
memset(m_lpszDefaultDir,0,_MAX_PATH);
LPITEMIDLIST pidl=NULL;
SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);
if (pidl && SHGetPathFromIDList(pidl, szDocument))
{
GetShortPathName(szDocument,m_lpszDefaultDir,_MAX_PATH);
}
如果需要,请在StdAfx.h中添加 #include <shlobj.h>
TCHAR MyDir[_MAX_PATH];
SHGetSpecialFolderPath(this->GetSafeHwnd(),MyDir,CSIDL_APPDATA,0);
AfxMessageBox(MyDir);
其中第三个参数CSIDL_APPDATA可以为以下的值:
CSIDL_BITBUCKET 回收站
CSIDL_CONTROLS 控制面板
CSIDL_DESKTOP Windows 桌面Desktop
CSIDL_DESKTOPDIRECTORY Desktop的目录
CSIDL_DRIVES 我的电脑
CSIDL_FONTS 字体目录
CSIDL_NETHOOD 网上邻居
CSIDL_NETWORK 网上邻居虚拟目录
CSIDL_PERSONAL 我的文档
CSIDL_PRINTERS 打印机
CSIDL_PROGRAMS 程序组
CSIDL_RECENT 最近打开的文档
CSIDL_SENDTO “发送到”菜单项
CSIDL_STARTMENU 任务条启动菜单项
CSIDL_STARTUP 启动目录
CSIDL_TEMPLATES 文档模板
//如果下面的值在VC开发环境中不存在话,可以在VC98/include/SHLOBJ.H中定义该变量
CSIDL_FLAG_CREATE(0x8000)
Version 5.0. Combine this CSIDL with any of the following CSIDLs to force the creation of the associated folder.
CSIDL_ADMINTOOLS(0x0030)
Version 5.0. The file system directory that is used to store administrative tools for an individual user. The Microsoft Management Console (MMC) will save customized consoles to this directory, and it will roam with the user.
CSIDL_ALTSTARTUP(0x001d)
The file system directory that corresponds to the user's nonlocalized Startup program group.
CSIDL_APPDATA(0x001a)
Version 4.71. The file system directory that serves as a common repository for application-specific data. A typical path is C:Documents and SettingsusernameApplication Data. This CSIDL is supported by the redistributable Shfolder.dll for systems that do not have the Microsoft® Internet Explorer 4.0 integrated Shell installed.
CSIDL_BITBUCKET(0x000a)
The virtual folder containing the objects in the user's Recycle Bin.
CSIDL_CDBURN_AREA(0x003b)
Version 6.0. The file system directory acting as a staging area for files waiting to be written to CD. A typical path is C:Documents and SettingsusernameLocal SettingsApplication DataMicrosoftCD Burning.
CSIDL_COMMON_ADMINTOOLS(0x002f)
Version 5.0. The file system directory containing administrative tools for all users of the computer.
CSIDL_COMMON_ALTSTARTUP(0x001e)
The file system directory that corresponds to the nonlocalized Startup program group for all users. Valid only for Microsoft Windows NT® systems.
CSIDL_COMMON_APPDATA(0x0023)
Version 5.0. The file system directory containing application data for all users. A typical path is C:Documents and SettingsAll UsersApplication Data.
CSIDL_COMMON_DESKTOPDIRECTORY(0x0019) //获取桌面
CSIDL_COMMON_DOCUMENTS(0x002e)
The file system directory that contains documents that are common to all users. A typical paths is C:Documents and SettingsAll UsersDocuments. Valid for Windows NT systems and Microsoft Windows® 95 and Windows 98 systems with Shfolder.dll installed.
CSIDL_COMMON_FAVORITES(0x001f) //获取收藏文件夹
The file system directory that serves as a common repository for favorite items common to all users. Valid only for Windows NT systems.
CSIDL_COMMON_MUSIC(0x0035)
Version 6.0. The file system directory that serves as a repository for music files common to all users. A typical path is C:Documents and SettingsAll UsersDocumentsMy Music.
CSIDL_COMMON_PICTURES(0x0036)
Version 6.0. The file system directory that serves as a repository for image files common to all users. A typical path is C:Documents and SettingsAll UsersDocumentsMy Pictures.
CSIDL_COMMON_PROGRAMS(0x0017) //获取程序菜单目录
The file system directory that contains the directories for the common program groups that appear on the Start menu for all users. A typical path is C:Documents and SettingsAll UsersStart MenuPrograms. Valid only for Windows NT systems.
CSIDL_COMMON_STARTMENU(0x0016) //开始菜单
The file system directory that contains the programs and folders that appear on the Start menu for all users. A typical path is C:Documents and SettingsAll UsersStart Menu. Valid only for Windows NT systems.
CSIDL_COMMON_STARTUP(0x0018) //获取启动菜单
The file system directory that contains the programs that appear in the Startup folder for all users. A typical path is C:Documents and SettingsAll UsersStart MenuProgramsStartup. Valid only for Windows NT systems.
CSIDL_COMMON_TEMPLATES(0x002d) //临时文件夹
The file system directory that contains the templates that are available to all users. A typical path is C:Documents and SettingsAll UsersTemplates. Valid only for Windows NT systems.
CSIDL_COMMON_VIDEO(0x0037)
Version 6.0. The file system directory that serves as a repository for video files common to all users. A typical path is C:Documents and SettingsAll UsersDocumentsMy Videos.
CSIDL_CONTROLS(0x0003)
The virtual folder containing icons for the Control Panel applications.
CSIDL_COOKIES(0x0021) //获取Cookies目录
The file system directory that serves as a common repository for Internet cookies. A typical path is C:Documents and SettingsusernameCookies.
CSIDL_DESKTOP(0x0000) //获取桌面
The virtual folder representing the Windows desktop, the root of the namespace.
CSIDL_DESKTOPDIRECTORY(0x0010) //获取桌面
The file system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself). A typical path is C:Documents and SettingsusernameDesktop.
CSIDL_DRIVES(0x0011)
The virtual folder representing My Computer, containing everything on the local computer: storage devices, printers, and Control Panel. The folder may also contain mapped network drives.
CSIDL_FAVORITES(0x0006)
The file system directory that serves as a common repository for the user's favorite items. A typical path is C:Documents and SettingsusernameFavorites.
CSIDL_FONTS(0x0014) //获取字体目录
A virtual folder containing fonts. A typical path is C:WindowsFonts.
CSIDL_HISTORY(0x0022) //获取历史目录
The file system directory that serves as a common repository for Internet history items.
CSIDL_INTERNET(0x0001)
A virtual folder representing the Internet.
CSIDL_INTERNET_CACHE(0x0020)
Version 4.72. The file system directory that serves as a common repository for temporary Internet files. A typical path is C:Documents and SettingsusernameLocal SettingsTemporary Internet Files.
CSIDL_LOCAL_APPDATA(0x001c)
Version 5.0. The file system directory that serves as a data repository for local (nonroaming) applications. A typical path is C:Documents and SettingsusernameLocal SettingsApplication Data.
CSIDL_MYDOCUMENTS(0x000c)
Version 6.0. The virtual folder representing the My Documents desktop item. This should not be confused with CSIDL_PERSONAL, which represents the file system folder that physically stores the documents.
CSIDL_MYMUSIC(0x000d)
The file system directory that serves as a common repository for music files. A typical path is C:Documents and SettingsUserMy DocumentsMy Music.
CSIDL_MYPICTURES(0x0027)
Version 5.0. The file system directory that serves as a common repository for image files. A typical path is C:Documents and SettingsusernameMy DocumentsMy Pictures.
CSIDL_MYVIDEO(0x000e)
Version 6.0. The file system directory that serves as a common repository for video files. A typical path is C:Documents and SettingsusernameMy DocumentsMy Videos.
CSIDL_NETHOOD(0x0013)
A file system directory containing the link objects that may exist in the My Network Places virtual folder. It is not the same as CSIDL_NETWORK, which represents the network namespace root. A typical path is C:Documents and SettingsusernameNetHood.
CSIDL_NETWORK(0x0012)
A virtual folder representing Network Neighborhood, the root of the network namespace hierarchy.
CSIDL_PERSONAL(0x0005)
The file system directory used to physically store a user's common repository of documents. A typical path is C:Documents and SettingsusernameMy Documents. This should be distinguished from the virtual My Documents folder in the namespace, identified by CSIDL_MYDOCUMENTS. To access that virtual folder, use SHGetFolderLocation, which returns the ITEMIDLIST for the virtual location, or refer to the technique described in Managing the File System.
CSIDL_PRINTERS(0x0004)
The virtual folder containing installed printers.
CSIDL_PRINTHOOD(0x001b)
The file system directory that contains the link objects that can exist in the Printers virtual folder. A typical path is C:Documents and SettingsusernamePrintHood.
CSIDL_PROFILE(0x0028)
Version 5.0. The user's profile folder. A typical path is C:Documents and Settingsusername. Applications should not create files or folders at this level; they should put their data under the locations referred to by CSIDL_APPDATA or CSIDL_LOCAL_APPDATA.
CSIDL_PROFILES(0x003e)
Version 6.0. The file system directory containing user profile folders. A typical path is C:Documents and Settings.
CSIDL_PROGRAM_FILES(0x0026)
Version 5.0. The Program Files folder. A typical path is C:Program Files.
CSIDL_PROGRAM_FILES_COMMON(0x002b)
Version 5.0. A folder for components that are shared across applications. A typical path is C:Program FilesCommon. Valid only for Windows NT, Windows 2000, and Windows XP systems. Not valid for Windows Millennium Edition (Windows Me).
CSIDL_PROGRAMS(0x0002)
The file system directory that contains the user's program groups (which are themselves file system directories). A typical path is C:Documents and SettingsusernameStart MenuPrograms.
CSIDL_RECENT(0x0008)
The file system directory that contains shortcuts to the user's most recently used documents. A typical path is C:Documents and SettingsusernameMy Recent Documents. To create a shortcut in this folder, use SHAddToRecentDocs. In addition to creating the shortcut, this function updates the Shell's list of recent documents and adds the shortcut to the My Recent Documents submenu of the Start menu.
CSIDL_SENDTO(0x0009)
The file system directory that contains Send To menu items. A typical path is C:Documents and SettingsusernameSendTo.
CSIDL_STARTMENU (0x000b) ///开始菜单
The file system directory containing Start menu items. A typical path is C:Documents and SettingsusernameStart Menu.
CSIDL_STARTUP(0x0007)
The file system directory that corresponds to the user's Startup program group. The system starts these programs whenever any user logs onto Windows NT or starts Windows 95. A typical path is C:Documents and SettingsusernameStart MenuProgramsStartup.
CSIDL_SYSTEM(0x0025) ///系统目录
Version 5.0. The Windows System folder. A typical path is C:WindowsSystem32.
CSIDL_TEMPLATES(0x0015)
The file system directory that serves as a common repository for document templates. A typical path is C:Documents and SettingsusernameTemplates.
CSIDL_WINDOWS(0x0024) ///Windows目录
Version 5.0. The Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables. A typical path is C:Windows.