Vibeeshan Mahadeva
Vibeeshan Mahadeva

Reputation: 7238

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)

Upvotes: 13

Views: 20749

Answers (2)

Alister
Alister

Reputation: 6837

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

Upvotes: 28

Marjan Venema
Marjan Venema

Reputation: 19346

CSIDL_PERSONAL is the My Documents folder:

CSIDL_PERSONAL FOLDERID_Documents Version 6.0. The virtual folder that represents the My Documents desktop item. This is equivalent to CSIDL_MYDOCUMENTS.

Previous to Version 6.0. The file system directory used to physically store a user's common repository of documents. A typical path is C:\Documents and Settings\username\My Documents. This should be distinguished from the virtual My Documents folder in the namespace. To access that virtual folder, use SHGetFolderLocation, which returns the ITEMIDLIST for the virtual location, or refer to the technique described in Managing the File System.Managing the File System.

See: http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx for a list and description of all CSIDL constants available

Upvotes: 17

Related Questions