{---------------------------------------- システムフォルダパスを扱うユニット 2006/01/31 ・ 作成 ・ uses ShlObjが必要(CSIDL_??の定数定義の為) 2010/09/16(木) ・ TSysUserFolderとTSysAllUserFolderを統合して 全体的にリファクタリングした。 //----------------------------------------} unit SpecialFolderPathUnit; interface uses Registry, Windows, uses_end; type TSpecialFolder = ( sfUserAppData, sfUserCache, sfUserCD_Burning, sfUserCookies, sfUserDesktop, sfUserFavorites, sfUserFonts, sfUserHistory, sfUserLocal_AppData, sfUserLocal_Settings, sfUserMy_Music, sfUserMy_Pictures, sfUserMy_Video, sfUserNetHood, sfUserPersonal, sfUserPrintHood, sfUserPrograms, sfUserRecent, sfUserSendTo, sfUserStart_Menu, sfUserStartup, sfUserTemplates, sfAllUserCommon_Administrative_Tools, sfAllUserCommon_AppData, sfAllUserCommon_Desktop, sfAllUserCommon_Documents, sfAllUserCommon_Favorites, sfAllUserCommon_Programs, sfAllUserCommon_Start_Menu, sfAllUserCommon_Startup, sfAllUserCommon_Templates, sfAllUserCommonMusic, sfAllUserCommonPictures, sfAllUserCommonVideo ); function GetSpecialFolderKeyName(Value: TSpecialFolder): String; function GetSpecialFolderPath(Value: TSpecialFolder): String; function WindowsFolderPath: String; function SystemFolderPath: String; implementation uses SysUtils; {--------------------------------------- システムフォルダのパスを返す関数 機能: レジストリからパスを読み取る 有効な文字列はソースの通り(WinXP環境) 備考: 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しか見れないので HKEY_LOCAL_MACHINEを見るためにTRegistryを使っている。 履歴: 2005/10/26 2010/09/17 ・ GetSystemUserFolderPath/GetSystemAllUserFolderPathを 内部関数化して列挙型でまとめて GetSpecialFolderKey/GetSpecialFolderPathを使うようにした。 }//(*----------------------------------- function GetSystemUserFolderPath(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 GetSystemAllUserFolderPath(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; function GetSystemUserFolderKey(Value: TSpecialFolder): String; begin case Value of sfUserAppData: Result := 'AppData'; sfUserCache: Result := 'Cache'; sfUserCD_Burning: Result := 'CD Burning'; sfUserCookies: Result := 'Cookies'; sfUserDesktop: Result := 'Desktop'; sfUserFavorites: Result := 'Favorites'; sfUserFonts: Result := 'Fonts'; sfUserHistory: Result := 'History'; sfUserLocal_AppData: Result := 'Local AppData'; sfUserLocal_Settings: Result := 'Local Settings'; sfUserMy_Music: Result := 'My Music'; sfUserMy_Pictures: Result := 'My Pictures'; sfUserMy_Video: Result := 'My Video'; sfUserNetHood: Result := 'NetHood'; sfUserPersonal: Result := 'Personal'; sfUserPrintHood: Result := 'PrintHood'; sfUserPrograms: Result := 'Programs'; sfUserRecent: Result := 'Recent'; sfUserSendTo: Result := 'SendTo'; sfUserStart_Menu: Result := 'Start Menu'; sfUserStartup: Result := 'Startup'; sfUserTemplates: Result := 'Templates'; else Assert(False, Format('%s:正しくない型です', ['GetSystemUserFolderKey'])); Result := ''; end; end; {---------------------------------------- Administrative Tools はキーは存在するが値が無いようです。 //----------------------------------------} function GetSystemAllUserFolderKey(Value: TSpecialFolder): String; begin case Value of sfAllUserCommon_Administrative_Tools: Result := 'Common Administrative Tools'; sfAllUserCommon_AppData: Result := 'Common AppData'; sfAllUserCommon_Desktop: Result := 'Common Desktop'; sfAllUserCommon_Documents: Result := 'Common Documents'; sfAllUserCommon_Favorites: Result := 'Common Favorites'; sfAllUserCommon_Programs: Result := 'Common Programs'; sfAllUserCommon_Start_Menu: Result := 'Common Start Menu'; sfAllUserCommon_Startup: Result := 'Common Startup'; sfAllUserCommon_Templates: Result := 'Common Templates'; sfAllUserCommonMusic: Result := 'CommonMusic'; sfAllUserCommonPictures: Result := 'CommonPictures'; sfAllUserCommonVideo: Result := 'CommonVideo'; else Assert(False, Format('%s:正しくない型です', ['GetSystemAllUserFolderKey'])); Result := ''; end; end; function GetSpecialFolderKeyName(Value: TSpecialFolder): String; begin case Value of sfUserAppData, sfUserCache, sfUserCD_Burning, sfUserCookies, sfUserDesktop, sfUserFavorites, sfUserFonts, sfUserHistory, sfUserLocal_AppData, sfUserLocal_Settings, sfUserMy_Music, sfUserMy_Pictures, sfUserMy_Video, sfUserNetHood, sfUserPersonal, sfUserPrintHood, sfUserPrograms, sfUserRecent, sfUserSendTo, sfUserStart_Menu, sfUserStartup, sfUserTemplates: Result := GetSystemUserFolderKey(Value); sfAllUserCommon_Administrative_Tools, sfAllUserCommon_AppData, sfAllUserCommon_Desktop, sfAllUserCommon_Documents, sfAllUserCommon_Favorites, sfAllUserCommon_Programs, sfAllUserCommon_Start_Menu, sfAllUserCommon_Startup, sfAllUserCommon_Templates, sfAllUserCommonMusic, sfAllUserCommonPictures, sfAllUserCommonVideo: Result := GetSystemAllUserFolderKey(Value); else Assert(False, Format('%s:存在しない型です', ['GetSpecialFolderKey'])); Result := ''; end; end; function GetSpecialFolderPath(Value: TSpecialFolder): String; begin case Value of sfUserAppData, sfUserCache, sfUserCD_Burning, sfUserCookies, sfUserDesktop, sfUserFavorites, sfUserFonts, sfUserHistory, sfUserLocal_AppData, sfUserLocal_Settings, sfUserMy_Music, sfUserMy_Pictures, sfUserMy_Video, sfUserNetHood, sfUserPersonal, sfUserPrintHood, sfUserPrograms, sfUserRecent, sfUserSendTo, sfUserStart_Menu, sfUserStartup, sfUserTemplates: Result := GetSystemUserFolderPath(GetSystemUserFolderKey(Value)); sfAllUserCommon_Administrative_Tools, sfAllUserCommon_AppData, sfAllUserCommon_Desktop, sfAllUserCommon_Documents, sfAllUserCommon_Favorites, sfAllUserCommon_Programs, sfAllUserCommon_Start_Menu, sfAllUserCommon_Startup, sfAllUserCommon_Templates, sfAllUserCommonMusic, sfAllUserCommonPictures, sfAllUserCommonVideo: Result := GetSystemAllUserFolderPath(GetSystemAllUserFolderKey(Value)); else Assert(False, Format('%s:存在しない型です', ['GetSpecialFolderPath'])); Result := ''; end; end; //------------------------------------*) {--------------------------------------- Windowsフォルダのパスを得る関数 機能: 備考: WindowsとSystemだけは専用の呼び出しAPIがある 履歴: 2009/06/10(水) 22:20 ・ 作成 }//(*----------------------------------- function WindowsFolderPath: String; var iDirectory: array[0..MAX_PATH-1] of Char; begin GetWindowsDirectory(iDirectory, MAX_PATH); //Windowsフォルダ Result := iDirectory; end; function SystemFolderPath: String; var iDirectory: array[0..MAX_PATH-1] of Char; begin GetSystemDirectory(iDirectory, MAX_PATH); //Systemフォルダ Result := iDirectory; end; //------------------------------------*) end.