Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get My Documents folder path in delphi

i use the following code to get special directories

uses
  ActiveX, ShlObj;

{...}

procedure TForm1.Button1Click(Sender: TObject);
// Replace CSIDL_HISTORY with the constants below
var
  Allocator: IMalloc;
  SpecialDir: PItemIdList;
  FBuf: array[0..MAX_PATH] of Char;
  PerDir: string;
begin
  if SHGetMalloc(Allocator) = NOERROR then
  begin
    SHGetSpecialFolderLocation(Form1.Handle, CSIDL_PERSONAL, SpecialDir);
    SHGetPathFromIDList(SpecialDir, @FBuf[0]);
    Allocator.Free(SpecialDir);
    ShowMessage(string(FBuf));
  end;
end;

And now i want to get the my documents path so i use mydocfolderpath := string(FBuf) + '\Documents' and i think it works well but my doubt is this the mydocuments path on all windows PCs (personalfolder/documents) can the user change this stucture and make my documents folder anywhare else (eg: c:\documents) if the user an change the path give me a proper way and i like to know what is the name of mydocuments folder (My Documents or Documents)

like image 536
VibeeshanRC Avatar asked Nov 01 '10 10:11

VibeeshanRC


1 Answers

If you are using a recent version of Delphi (XE5 or greater) then you can use the new platform agnostic classes. Basically include System.IOUtils in your uses then use TPath.GetDocumentsPath to get the documents folder.

Check out the Doc Wiki

like image 148
Alister Avatar answered Sep 28 '22 02:09

Alister