Windowsの特殊フォルダのパスを取得するには、SHGetSpecialFolderPath()関数を呼び出します。 第3引数に渡した、CSIDL_DESKTOPなどの値によって様々な特殊フォルダの情報を取得することができます。
| 第3引数 | 取得できる内容 |
|---|---|
| CSIDL_DESKTOP | デスクトップのパス |
| CSIDL_DESKTOPDIRECTORY | デスクトップのパス |
| CSIDL_STARTMENU | スタートメニューのパス |
| CSIDL_PROGRAMS | Program Filesのパス |
| CSIDL_STARTUP | Startupのパス |
| CSIDL_ALTSTARTUP | Startup(AllUser)のパス |
| CSIDL_PERSONAL | My Documentsのパス |
| CSIDL_FAVORITES | お気に入りのパス |
| CSIDL_RECENT | ゴミ箱のパスを取得 |
| CSIDL_SENDTO | Sendtoのパス |
| CSIDL_NETHOOD | NetHoodのバス |
| CSIDL_FONTS | Fontsのバス |
| CSIDL_TEMPLATES | ShellNewのバス |
| CSIDL_APPDATA | AppDataのバス |
| CSIDL_PRINTHOOD | PrintHoodのバス |
| CSIDL_INTERNET_CACHE | INetキャッシュのパス |
| CSIDL_COOKIES | INet Cookiesのパス |
| CSIDL_HISTORY | 履歴のパス |
| CSIDL_COMMON_DESKTOPDIRECTORY | デスクトップ(AllUser)のパス |
| CSIDL_COMMON_STARTMENU | スタートメニュー(AllUser)のパス |
| CSIDL_COMMON_PROGRAMS | Program Files(AllUser)のパス |
| CSIDL_COMMON_STARTUP | スタートアップ(AllUser)のパス |
| CSIDL_COMMON_ALTSTARTUP | スタートアップ(AllUser)のパス |
| CSIDL_COMMON_FAVORITES | お気に入りのパス |
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <shlobj.h>
/*
Windowsの特殊フォルダのパスを取得する
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
/*
std::wcoutのロケールを設定
これを設定するだけで、std::wcoutで日本語が表示される
ようになります。
*/
std::wcout.imbue( std::locale( "", std::locale::ctype ) );
// パス取得バッファ
TCHAR waFolderPath[ MAX_PATH ];
// デスクトップのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_DESKTOP, 0 );
std::wcout << L"CSIDL_DESKTOP : " << waFolderPath << std::endl;
// デスクトップのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_DESKTOPDIRECTORY, 0 );
std::wcout << L"CSIDL_DESKTOPDIRECTORY : " << waFolderPath << std::endl;
// スタートメニューのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_STARTMENU, 0 );
std::wcout << L"CSIDL_STARTMENU : " << waFolderPath << std::endl;
// Program Filesのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_PROGRAMS, 0 );
std::wcout << L"CSIDL_PROGRAMS : " << waFolderPath << std::endl;
// Startupのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_STARTUP, 0 );
std::wcout << L"CSIDL_STARTUP : " << waFolderPath << std::endl;
// Startup(AllUser)のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_ALTSTARTUP, 0 );
std::wcout << L"CSIDL_ALTSTARTUP : " << waFolderPath << std::endl;
// My Documentsのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_PERSONAL, 0 );
std::wcout << L"CSIDL_PERSONAL : " << waFolderPath << std::endl;
// お気に入りのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_FAVORITES, 0 );
std::wcout << L"CSIDL_FAVORITES : " << waFolderPath << std::endl;
// ゴミ箱のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_RECENT, 0 );
std::wcout << L"CSIDL_RECENT : " << waFolderPath << std::endl;
// Sendtoのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_SENDTO, 0 );
std::wcout << L"CSIDL_SENDTO : " << waFolderPath << std::endl;
// NetHoodのバスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_NETHOOD, 0 );
std::wcout << L"CSIDL_NETHOOD : " << waFolderPath << std::endl;
// Fontsのバスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_FONTS, 0 );
std::wcout << L"CSIDL_FONTS : " << waFolderPath << std::endl;
// ShellNewのバスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_TEMPLATES, 0 );
std::wcout << L"CSIDL_TEMPLATES : " << waFolderPath << std::endl;
// AppDataのバスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_APPDATA, 0 );
std::wcout << L"CSIDL_APPDATA : " << waFolderPath << std::endl;
// PrintHoodのバスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_PRINTHOOD, 0 );
std::wcout << L"CSIDL_PRINTHOOD : " << waFolderPath << std::endl;
// INetキャッシュのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_INTERNET_CACHE, 0 );
std::wcout << L"CSIDL_INTERNET_CACHE : " << waFolderPath << std::endl;
// INet Cookiesのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COOKIES, 0 );
std::wcout << L"CSIDL_COOKIES : " << waFolderPath << std::endl;
// 履歴のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_HISTORY, 0 );
std::wcout << L"CSIDL_HISTORY : " << waFolderPath << std::endl;
// デスクトップ(AllUser)のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COMMON_DESKTOPDIRECTORY, 0 );
std::wcout << L"CSIDL_COMMON_DESKTOPDIRECTORY : " << waFolderPath << std::endl;
// スタートメニュー(AllUser)のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COMMON_STARTMENU, 0 );
std::wcout << L"CSIDL_COMMON_STARTMENU : " << waFolderPath << std::endl;
// Program Files(AllUser)のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COMMON_PROGRAMS, 0 );
std::wcout << L"CSIDL_COMMON_PROGRAMS : " << waFolderPath << std::endl;
// スタートアップ(AllUser)のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COMMON_STARTUP, 0 );
std::wcout << L"CSIDL_COMMON_STARTUP : " << waFolderPath << std::endl;
// スタートアップ(AllUser)のパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COMMON_ALTSTARTUP, 0 );
std::wcout << L"CSIDL_COMMON_ALTSTARTUP : " << waFolderPath << std::endl;
// お気に入りのパスを取得
SHGetSpecialFolderPath( NULL, waFolderPath, CSIDL_COMMON_FAVORITES, 0 );
std::wcout << L"CSIDL_COMMON_FAVORITES : " << waFolderPath << std::endl;
// 正常終了
return( 0 );
}
CSIDL_DESKTOP : C:\Users\testuser\Desktop CSIDL_DESKTOPDIRECTORY : C:\Users\testuser\Desktop CSIDL_STARTMENU : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Start Menu CSIDL_PROGRAMS : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs CSIDL_STARTUP : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup CSIDL_ALTSTARTUP : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup CSIDL_PERSONAL : C:\Users\testuser\Documents CSIDL_FAVORITES : C:\Users\testuser\Favorites CSIDL_RECENT : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Recent CSIDL_SENDTO : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\SendTo CSIDL_NETHOOD : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Network Shortcuts CSIDL_FONTS : C:\WINDOWS\Fonts CSIDL_TEMPLATES : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Templates CSIDL_APPDATA : C:\Users\testuser\AppData\Roaming CSIDL_PRINTHOOD : C:\Users\testuser\AppData\Roaming\Microsoft\Windows\Printer Shortcuts CSIDL_INTERNET_CACHE : C:\Users\testuser\AppData\Local\Microsoft\Windows\INetCache CSIDL_COOKIES : C:\Users\testuser\AppData\Local\Microsoft\Windows\INetCookies CSIDL_HISTORY : C:\Users\testuser\AppData\Local\Microsoft\Windows\History CSIDL_COMMON_DESKTOPDIRECTORY : C:\Users\Public\Desktop CSIDL_COMMON_STARTMENU : C:\ProgramData\Microsoft\Windows\Start Menu CSIDL_COMMON_PROGRAMS : C:\ProgramData\Microsoft\Windows\Start Menu\Programs CSIDL_COMMON_STARTUP : C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup CSIDL_COMMON_ALTSTARTUP : C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup CSIDL_COMMON_FAVORITES : C:\Users\testuser\Favorites