Nie mam Delphi 2009, ale pod Windows 7 oraz Delphi 7 ten kod działa ok...
//...
uses
ShlObj;
function AddBackSlash(Path : string) : string;
begin
Result := '';
if Path = '' then
begin
Exit;
end;
if Path[Length(Path)] <> '\' then
begin
Result := Path + '\'
end
else
begin
Result := Path;
end;
end;
function GetAppDataDirectory : string;
var
Pidl : PItemIDList;
Folder : array[0..MAX_PATH] of Char;
begin
SHGetSpecialFolderLocation(0, CSIDL_APPDATA, Pidl);
SHGetPathFromIDList(PIDL, Folder);
Result := AddBackSlash(Folder);
end;
Sprawdź sobie co zwróci:
Edit1.Text := GetAppDataDirectory;
Natomiast do ewentualnego tworzenia podkatalogów w podanej ścieżce stosuje zwykle taką funkcję jak poniżej, którą kiedyś wygooglowałem i nie zmieniałem jej, bo jak dla mnie działa idealnie.
function MakeDirectory(Dir : string) : boolean;
function Last(What : string; Where : string) : integer;
var
Ind : Integer;
begin
Result := 0;
for Ind := (Length(Where) - Length(What) + 1) downto 1 do
if Copy(Where, Ind, Length(What)) = What then
begin
Result := Ind;
Break;
end;
end;
var
Ind : integer;
PrevDir : string;
begin
Result := True;
if Copy(Dir, 2, 1) <> ':' then
if Copy(Dir, 3, 1) <> '\' then
if Copy(Dir, 1, 1) = '\' then
Dir := 'C:' + Dir
else
Dir := 'C:\' + Dir
else
Dir := 'C:' + Dir;
if DirectoryExists(Dir) = False then
begin
Ind := Last('\', Dir);
PrevDir := Copy(Dir, 1, Ind - 1);
if DirectoryExists(PrevDir) = False then
begin
MakeDirectory(PrevDir);
end;
Result := CreateDir(Dir);
end;
end;