본문 바로가기

운동하는 개발자/Delphi

delphi 각종 윈도우 경로 읽기 get windows path / SHGetSpecialFolderPath

728x90

uses
	shlObj;

SHGetSpecialFolderPath([핸들], [리턴받을 경로], [찾을 폴더명], [생성여부]

이 함수에서 찾을 폴더명(CSIDL)을 아래 링크를 참조하여 찾아보면 된다

 

사용 예시)

function GetWindowsPath(nCSIDL): String;
var
  P: array [0..MAX_PATH] of Char;
begin
  if SHGetSpecialFolderPath(0, @P[0], nCSIDL, True) then Result := P
  else                                                   Result := '';
end;



sAppdataPath := GetWindowsPath(CSIDL_APPDATA);
sProgramPath := GetWindowsPath(CSIDL_PROGRAM_FILES);

위와같이 함수를 복붙해서 만들어놓고 쓰면 편하다 

 

CSIDL값 참조는 아래링크

https://docs.microsoft.com/en-us/windows/win32/shell/csidl

 

CSIDL (Shlobj.h) - Win32 apps

CSIDL (constant special item ID list) values provide a unique system-independent way to identify special folders used frequently by applications, but which may not have the same name or location on any given system.

docs.microsoft.com

 

※추가로 과거에는 SHGetSpecialFolderPath 함수가 아닌 SHGetFolderPath를 사용했는데 
windows vista와 7이후 부터는 SHGetSpecialFolderPath 함수를 사용해야 정확한 Path를 가져 올 수 있다고 한다
(정확하진않지만 program files, program files (x86), programdata 등을 얘기하는건 아닐까 싶다)


 

728x90