SHGetSpecialFolderPath用法

时间:2023-03-09 17:42:52
SHGetSpecialFolderPath用法

The SHGetSpecialFolderPath function retrieves the path of a special folder that is identified by its CSIDL.

Syntax

BOOL SHGetSpecialFolderPath(
HWND hwndOwner,
LPTSTR lpszPath,
int nFolder,
BOOL fCreate
);

Parameters

hwndOwner
[in] Handle to the owner window the client should specify if it displays a dialog box or message box.
lpszPath
[in] Reference to a character buffer that receives the drive and path of the specified folder. This buffer must be at least MAX_PATH characters in size.
nFolder
[in] CSIDL that identifies the folder of interest. If a virtual folder is specified, this function fails. See Remarks for possible values.
fCreate
[in] Indicates whether the folder should be created if it does not already exist. If this value is nonzero, the folder is created. If this value is zero, the folder is not created.

Return Values

For Windows Mobile 2003 and later, returns TRUE if successful, FALSE otherwise. For Windows Mobile 2002 and earlier, returns FALSE even if successful. If the folder represented by the nFolder parameter does not exist and is not created, a NULL string is returned indicating that the directory does not exist.

Remarks

A number of folders are used frequently by applications but might not have the same name or location on any given system. CSIDL values provide a system-independent way to identify these special folders. These values supersede the use of environment variables for this purpose.

The following table lists valid CSIDL values for the nFolder parameter:

CSIDL Value Description
CSIDL_DESKTOP 0x0000 Not supported on Smartphone.
CSIDL_FAVORITES 0x0006 The file system directory that serves as a common repository for the user's favorite items.
CSIDL_FONTS 0x0014 The virtual folder that contains fonts.
CSIDL_PERSONAL 0x0005 The file system directory that serves as a common repository for documents.
CSIDL_PROGRAM_FILES 0x0026 The program files folder.
CSIDL_PROGRAMS 0x0002 The file system directory that contains the user's program groups, which are also file system directories.
CSIDL_STARTUP 0x0007 The file system directory that corresponds to the user's Startup program group. The system starts these programs when a device is powered on.
CSIDL_WINDOWS 0x0024 The Windows folder.

The CSIDL_DESKTOP value is invalid for the Smartphone platform. Smartphone uses a home screen instead of a desktop; do not use this CSIDL value within the Smartphone development environment.

  进行 Shell 程序的设计,需要使用一些头文件和库文件。

一般 Shell API 都在 shlobj.h 头文件中声明,由 Shell32.dll 导出,链接时需要使用到 Shell32.lib 库。

 C++ Code 
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
 
/*************************************
* VOID GetSpecialFolderQS()
* 功能    SHGetSpecialFolderPath用法
*
* 参数    未使用
**************************************/
VOID GetSpecialFolderQS()
{
    CHAR szDeskTop[MAX_PATH]          = {};
    CHAR szFavourites[MAX_PATH]       = {};
    CHAR szFonts[MAX_PATH]             = {};
    CHAR szMyDocument[MAX_PATH]     = {};
    CHAR szProgramFiles[MAX_PATH]   = {};
    CHAR szPrograms[MAX_PATH]       = {};
    CHAR szStartUp[MAX_PATH]        = {};
    CHAR szWindows[MAX_PATH]        = {};

// 使用SHGetSpecialFolderPath获取特殊目录路径
    SHGetSpecialFolderPath(NULL, szDeskTop,         CSIDL_DESKTOP,          FALSE);
    SHGetSpecialFolderPath(NULL, szFavourites,      CSIDL_FAVORITES,        FALSE);
    SHGetSpecialFolderPath(NULL, szFonts,           CSIDL_FONTS,            FALSE);
    SHGetSpecialFolderPath(NULL, szMyDocument,      CSIDL_PERSONAL,         FALSE);
    SHGetSpecialFolderPath(NULL, szProgramFiles,    CSIDL_PROGRAM_FILES,    FALSE);
    SHGetSpecialFolderPath(NULL, szPrograms,        CSIDL_PROGRAMS,         FALSE);
    SHGetSpecialFolderPath(NULL, szStartUp,         CSIDL_STARTUP,          FALSE);
    SHGetSpecialFolderPath(NULL, szWindows,         CSIDL_WINDOWS,          FALSE);
    printf("DeskTop:\t %s\n",       szDeskTop);
    printf("Favourites:\t %s\n",    szFavourites);
    printf("Fonts:\t %s\n",         szFonts);
    printf("My Document:\t %s\n",   szMyDocument);
    printf("Program Files:\t %s\n", szProgramFiles);
    printf("Programs:\t %s\n",      szPrograms);
    printf("StartUp:\t %s\n",       szStartUp);
    printf("Windows:\t %s\n",       szWindows);
}

SHGetSpecialFolderPath用法