取得windows中一些特殊文件夹路径的方法

比如当前用户的documents文件夹、windows安装文件夹等,虽然一些软件的默认文档保存位置通过这种方式取得感觉有点恶心,不过在用户未指定默认目录的情况下通过这种方式取得的路径还是比较合理的。

以下内容转抄自Visual C++ Tips,原始链接:http://weseetips.com/2008/05/01/how-to-get-the-path-of-special-folders-in-windows/

Windows have a number of special folder such as my documents, desktop folder etc. They are special because, their path can be different in system to system. So how can you get the path of special folder in windows?

You can use the api – SHGetSpecialFolderPath(). For each special folder there is a predefined ID calledCSIDL. For instance, for windows system directory the CSIDL is CSIDL_WINDOWS. While calling the function, you’ve to provide the string buffer and the CSIDL and the requested special folder path is retuned in specified string buffer. See code snippet which fetches the path of Desktop directory.

// String buffer for holding the path.
TCHAR strPath[ MAX_PATH ];

// Get the special folder path.
SHGetSpecialFolderPath(
    0,       // Hwnd
    strPath, // String buffer.
    CSIDL_DESKTOPDIRECTORY, // CSLID of folder
    FALSE ); // Create if doesn't exists?
Wanna to see the list of CSLID of special folders? have a look at them. Their names are self explanatory.

CSIDL_ADMINTOOLS
CSIDL_ALTSTARTUP
CSIDL_APPDATA
CSIDL_BITBUCKET
CSIDL_COMMON_ADMINTOOLS
CSIDL_COMMON_ALTSTARTUP
CSIDL_COMMON_APPDATA
CSIDL_COMMON_DESKTOPDIRECTORY
CSIDL_COMMON_DOCUMENTS
CSIDL_COMMON_FAVORITES
CSIDL_COMMON_PROGRAMS
CSIDL_COMMON_STARTMENU
CSIDL_COMMON_STARTUP
CSIDL_COMMON_TEMPLATES
CSIDL_CONTROLS
CSIDL_COOKIES
CSIDL_DESKTOP
CSIDL_DESKTOPDIRECTORY
CSIDL_DRIVES
CSIDL_FAVORITES
CSIDL_FONTS
CSIDL_HISTORY
CSIDL_INTERNET
CSIDL_INTERNET_CACHE
CSIDL_LOCAL_APPDATA
CSIDL_MYMUSIC
CSIDL_MYPICTURES
CSIDL_NETHOOD
CSIDL_NETWORK
CSIDL_PERSONAL
CSIDL_PRINTERS
CSIDL_PRINTHOOD
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

博主友情提示:

如您在评论中需要提及如QQ号、电子邮件地址或其他隐私敏感信息,欢迎使用>>博主专用加密工具v3<<处理后发布,原文只有博主可以看到。