2007-05-28

Windows API 获得用户目录

今天写一个程序,需要获得C:\Documents and Settings\<UserName>\Application Data这个目录的文件名. 百度了一下,发现了SHGetFolderPath的函数,示例代码:

#include <iostream>
#include <shlobj.h>

using namespace std;

int main(int argc, char* argv[])
{
char szPath[MAX_PATH];

if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, szPath)))
{
cout << szPath << endl;
}
return 0;
}

注意代码应链结shell32库,命令行的话用/linkshell32.lib

又查了下MSDN,原型为

HRESULT SHGetFolderPath( HWND hwndOwner,
int nFolder,
HANDLE hToken,
DWORD dwFlags,
LPTSTR pszPath
);

其中hToken可取以下值:

CSIDL_ADMINTOOLS
CSIDL_COMMON_ADMINTOOLS
CSIDL_APPDATA
CSIDL_COMMON_APPDATA
CSIDL_COMMON_DOCUMENTS
CSIDL_COOKIES
CSIDL_FLAG_CREATE
CSIDL_HISTORY
CSIDL_INTERNET_CACHE
CSIDL_LOCAL_APPDATA
CSIDL_MYPICTURES
CSIDL_PERSONAL
CSIDL_PROGRAM_FILES
CSIDL_PROGRAM_FILES_COMMON
CSIDL_SYSTEM
CSIDL_WINDOWS
发表评论