{ ----------------------------------- システムフォルダパスを扱うユニット 2006/01/31 ・ 作成 ・ uses ShlObjが必要(CSIDL_??の定数定義の為) //----------------------------------- } unit SystemFolderPathUnit; interface uses ShlObj, Registry, Windows; type TSysUserFolder = ( sufAppData, sufCookies, sufDesktop, sufFavorites, sufNetHood, sufPersonal, sufMy_Pictures, sufPrintHood, sufRecent, sufSendTo, sufStart_Menu, sufTemplates, sufPrograms, sufStartup, sufLocal_Settings, sufLocal_AppData, sufCache, sufHistory, sufFonts, sufAdministrative_Tools); TSysUserFolderKey = record KeyString: String; end; const SysUserFolderKey: array[TSysUserFolder] of TSysUserFolderKey = ((KeyString: 'AppData'), (KeyString: 'Cookies'), (KeyString: 'Desktop'), (KeyString: 'Favorites'), (KeyString: 'NetHood'), (KeyString: 'Personal'), (KeyString: 'My Pictures'), (KeyString: 'PrintHood'), (KeyString: 'Recent'), (KeyString: 'SendTo'), (KeyString: 'Start Menu'), (KeyString: 'Templates'), (KeyString: 'Programs'), (KeyString: 'Startup'), (KeyString: 'Local Settings'), (KeyString: 'Local AppData'), (KeyString: 'Cache'), (KeyString: 'History'), (KeyString: 'Fonts'), (KeyString: 'Administrative Tools')); function SystemUserFolderPath(Value: String): String; type TSysAllUserFolder = ( saufCommon_Start_Menu, saufCommon_Desktop, saufCommon_AppData, saufCommon_Programs, saufCommon_Documents, saufCommon_Administrative_Tools, saufCommon_Startup, saufCommon_Templates, saufPersonal, saufCommon_Favorites); TSysAllUserFolderKey = record KeyString: String; end; const SysAllUserFolderKey: array[TSysAllUserFolder] of TSysAllUserFolderKey = ((KeyString: 'Common Start Menu'), (KeyString: 'Common Desktop'), (KeyString: 'Common AppData'), (KeyString: 'Common Programs'), (KeyString: 'Common Documents'), (KeyString: 'Common Administrative Tools'), (KeyString: 'Common Startup'), (KeyString: 'Common Templates'), (KeyString: 'Personal'), (KeyString: 'Common Favorites')); function SystemAllUserFolderPath(Value: String): String; type TSHSysFolder = ( ssfCSIDL_ALTSTARTUP, ssfCSIDL_APPDATA, ssfCSIDL_BITBUCKET, ssfCSIDL_COMMON_ALTSTARTUP, ssfCSIDL_COMMON_DESKTOPDIRECTORY, ssfCSIDL_COMMON_FAVORITES, ssfCSIDL_COMMON_PROGRAMS, ssfCSIDL_COMMON_STARTMENU, ssfCSIDL_COMMON_STARTUP, ssfCSIDL_CONTROLS, ssfCSIDL_COOKIES, ssfCSIDL_DESKTOP, ssfCSIDL_DESKTOPDIRECTORY, ssfCSIDL_DRIVES, ssfCSIDL_FAVORITES, ssfCSIDL_FONTS, ssfCSIDL_HISTORY, ssfCSIDL_INTERNET, ssfCSIDL_INTERNET_CACHE, ssfCSIDL_NETHOOD, ssfCSIDL_NETWORK, ssfCSIDL_PERSONAL, ssfCSIDL_PRINTERS, ssfCSIDL_PRINTHOOD, ssfCSIDL_PROGRAMS, ssfCSIDL_RECENT, ssfCSIDL_SENDTO, ssfCSIDL_STARTMENU, ssfCSIDL_STARTUP, ssfCSIDL_TEMPLATES); TSHSysFolderKey = record Key: Integer; KeyString: String; end; const SHSysFolderKey: array[TSHSysFolder] of TSHSysFolderKey = ((Key: CSIDL_ALTSTARTUP; KeyString: 'CSIDL_ALTSTARTUP'), (Key: CSIDL_APPDATA; KeyString: 'CSIDL_APPDATA'), (Key: CSIDL_BITBUCKET; KeyString: 'CSIDL_BITBUCKET'), (Key: CSIDL_COMMON_ALTSTARTUP; KeyString: 'CSIDL_COMMON_ALTSTARTUP'), (Key: CSIDL_COMMON_DESKTOPDIRECTORY; KeyString: 'CSIDL_COMMON_DESKTOPDIRECTORY'), (Key: CSIDL_COMMON_FAVORITES; KeyString: 'CSIDL_COMMON_FAVORITES'), (Key: CSIDL_COMMON_PROGRAMS; KeyString: 'CSIDL_COMMON_PROGRAMS'), (Key: CSIDL_COMMON_STARTMENU; KeyString: 'CSIDL_COMMON_STARTMENU'), (Key: CSIDL_COMMON_STARTUP; KeyString: 'CSIDL_COMMON_STARTUP'), (Key: CSIDL_CONTROLS; KeyString: 'CSIDL_CONTROLS'), (Key: CSIDL_COOKIES; KeyString: 'CSIDL_COOKIES'), (Key: CSIDL_DESKTOP; KeyString: 'CSIDL_DESKTOP'), (Key: CSIDL_DESKTOPDIRECTORY; KeyString: 'CSIDL_DESKTOPDIRECTORY'), (Key: CSIDL_DRIVES; KeyString: 'CSIDL_DRIVES'), (Key: CSIDL_FAVORITES; KeyString: 'CSIDL_FAVORITES'), (Key: CSIDL_FONTS; KeyString: 'CSIDL_FONTS'), (Key: CSIDL_HISTORY; KeyString: 'CSIDL_HISTORY'), (Key: CSIDL_INTERNET; KeyString: 'CSIDL_INTERNET'), (Key: CSIDL_INTERNET_CACHE; KeyString: 'CSIDL_INTERNET_CACHE'), (Key: CSIDL_NETHOOD; KeyString: 'CSIDL_NETHOOD'), (Key: CSIDL_NETWORK; KeyString: 'CSIDL_NETWORK'), (Key: CSIDL_PERSONAL; KeyString: 'CSIDL_PERSONAL'), (Key: CSIDL_PRINTERS; KeyString: 'CSIDL_PRINTERS'), (Key: CSIDL_PRINTHOOD; KeyString: 'CSIDL_PRINTHOOD'), (Key: CSIDL_PROGRAMS; KeyString: 'CSIDL_PROGRAMS'), (Key: CSIDL_RECENT; KeyString: 'CSIDL_RECENT'), (Key: CSIDL_SENDTO; KeyString: 'CSIDL_SENDTO'), (Key: CSIDL_STARTMENU; KeyString: 'CSIDL_STARTMENU'), (Key: CSIDL_STARTUP; KeyString: 'CSIDL_STARTUP'), (Key: CSIDL_TEMPLATES; KeyString: 'CSIDL_TEMPLATES') ); function SHSystemFolderPath(Value: Integer): String; implementation {------------------------------- // システムフォルダのパスを返す関数 機能: レジストリ版 引数説明: 有効な文字列は次の通り SystemUserFolderPath AppData/Cookies/Desktop/Favorites/NetHood/Personal /My Pictures/PrintHood/Recent/SendTo/Start Menu /Templates/Programs/Startup/Local Settings/Local AppData /Cache/History/Fonts/Administrative Tools SystemAllUserFolderPath Common Start Menu/Common Desktop/Common AppData /Common Programs/Common Documents/Common Administrative Tools /Common Startup/Common Templates/Personal/Common Favorites 戻り値: ファイルパス Edit1.Text := SystemUserFolderPath(SysUserFolderKey[sufDesktop].KeyString); このように使用する 備考: uses Registryが必要 ユーザー毎の設定は HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders 全ユーザーの設定は HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders に記載されている TRegIniFileではHKEY_CURRENT_USERしか見れないので TRegistryを使ってHKEY_LOCAL_MACHINEを見ている。 履歴: 2005/10/26 //------------------------------} function SystemUserFolderPath(Value: String): String; var Regini: TRegIniFile; begin Regini := TRegIniFile.Create( 'Software\Microsoft\Windows\CurrentVersion\Explorer'); Regini.RootKey := HKEY_CURRENT_USER; Result := Regini.ReadString('Shell Folders', Value, ''); Regini.Free; end; function SystemAllUserFolderPath(Value: String): String; var Reg: TRegistry; begin Reg := TRegistry.Create; Reg.RootKey := HKEY_LOCAL_MACHINE; Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', False); Result := Reg.ReadString(Value); Reg.Free; end; //------------------------------ {------------------------------- // システムフォルダのパスを返す関数 機能: シェル版 引数説明: 有効な文字列は次の通り SHSystemFolderPath CSIDL_ALTSTARTUP/CSIDL_APPDATA/CSIDL_BITBUCKET /CSIDL_COMMON_ALTSTARTUP/CSIDL_COMMON_DESKTOPDIRECTORY /CSIDL_COMMON_FAVORITES/CSIDL_COMMON_PROGRAMS/CSIDL_COMMON_STARTMENU /CSIDL_COMMON_STARTUP/CSIDL_CONTROLS/CSIDL_COOKIES/CSIDL_DESKTOP /CSIDL_DESKTOPDIRECTORY/CSIDL_DRIVES/CSIDL_FAVORITES/CSIDL_FONTS /CSIDL_HISTORY/CSIDL_INTERNET/CSIDL_INTERNET_CACHE/CSIDL_NETHOOD /CSIDL_NETWORK/CSIDL_PERSONAL/CSIDL_PRINTERS/CSIDL_PRINTHOOD /CSIDL_PROGRAMS/CSIDL_RECENT/CSIDL_SENDTO/CSIDL_STARTMENU /CSIDL_STARTUP/CSIDL_TEMPLATES/ 戻り値: ファイルパス Edit1.Text := SHSystemFolderPath(SHSysFolderKey[ssfCSIDL_DESKTOP].Key) このように使用する 備考: uses ShlObjが必要 IE4をShell統合でインストールした Win95,NT4かWin98,2000以降が必要 履歴: 2005/10/26 //------------------------------} function SHSystemFolderPath(Value: Integer): String; var lpszPath: array[0..MAX_PATH] of Char; begin SHGetSpecialFolderPath(HANDLE_FLAG_INHERIT, lpszPath, Value, False); Result := lpszPath; end; //------------------------------ end.