Detect user folders in Windows and Linux lazarus / 21-General ---------------------------------------------------------------------- Ericktux · 2025-03-31 00:23 Hi friends, I'd like to share some code that's helping me detect user folders. It works on Windows 7, 8, 10, 11, Ubuntu, Mint, and Debian. I hope it helps. Best regards, friends.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms,  {$IFDEF Windows}ShlObj, Windows, ActiveX, ComObj,ShellApi,{$ENDIF}  {$IFDEF Linux}Process,{$ENDIF}  LazFileUtils, LCLType,  Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    function GetUserSpecialDir(FolderType: Integer): String;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } {$IFDEF Windows}const  FOLDERID_Downloads: TGUID = '{374DE290-123F-4565-9164-39C4925E467B}';  FOLDERID_Documents: TGUID = '{FDD39AD0-238F-46AF-ADB4-6C85480369C7}';  FOLDERID_Music: TGUID = '{4BD8D571-6D19-48D3-BE97-422220080E43}';  FOLDERID_Videos: TGUID = '{18989B1D-99B5-455B-841C-AB7C74E4DDFC}';  FOLDERID_Pictures: TGUID = '{33E28130-4E1E-4676-835A-98395C3BC3BB}';  FOLDERID_Desktop: TGUID = '{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}'; function SHGetKnownFolderPath(const rfid: TGUID; dwFlags: DWORD; hToken: THandle;  out ppszPath: PWideChar): HResult; stdcall; external 'Shell32.dll'; function GetWindowsKnownFolder(const FolderID: TGUID): String;var  Path: PWideChar;begin  if SHGetKnownFolderPath(FolderID, 0, 0, Path) = S_OK then  begin    Result := UTF8Encode(WideString(Path));    CoTaskMemFree(Path);  end  else    Result := '';end;{$ENDIF} function TForm1.GetUserSpecialDir(FolderType: Integer): String;var  {$IFDEF Linux}  Process: TProcess;  Output: TStringList;  DirNames: array[1..6] of String = ('DOCUMENTS', 'DOWNLOAD', 'MUSIC', 'VIDEOS', 'PICTURES', 'DESKTOP');  {$ENDIF}  {$IFDEF WINDOWS}  HomeDir: string;  Path: array[0..MAX_PATH] of Char;  {$ENDIF}begin  Result := IncludeTrailingPathDelimiter(GetUserDir);   {$IFDEF Windows}  // Usamos las constantes de Windows para carpetas especiales (independiente del idioma)  case FolderType of    1: Result := GetWindowsKnownFolder(FOLDERID_Documents);  // Documentos    2: Result := GetWindowsKnownFolder(FOLDERID_Downloads);  // Descargas    3: Result := GetWindowsKnownFolder(FOLDERID_Music);      // Música    4: Result := GetWindowsKnownFolder(FOLDERID_Videos);     // Videos    5: Result := GetWindowsKnownFolder(FOLDERID_Pictures);   // Imágenes    6: Result := GetWindowsKnownFolder(FOLDERID_Desktop);    // Escritorio  end;  {$ENDIF}   {$IFDEF Linux}  // Usamos xdg-user-dir (obtiene la ruta REAL sin importar el idioma)  Process := TProcess.Create(nil);  Output := TStringList.Create;  try    Process.Executable := 'sh';    Process.Parameters.Add('-c');    Process.Parameters.Add('xdg-user-dir ' + DirNames[FolderType]);    Process.Options := [poWaitOnExit, poUsePipes];    Process.Execute;    Output.LoadFromStream(Process.Output);    Result := Trim(Output.Text);  finally    Output.Free;    Process.Free;  end;  {$ENDIF}end; procedure TForm1.Button1Click(Sender: TObject);begin  ShowMessage(    'Rutas del usuario:' + #13#10 + #13#10 +    'Usuario: ' + GetUserDir + #13#10 +    'Documentos: ' + GetUserSpecialDir(1) + #13#10 +    'Descargas: ' + GetUserSpecialDir(2) + #13#10 +    'Música: ' + GetUserSpecialDir(3) + #13#10 +    'Videos: ' + GetUserSpecialDir(4) + #13#10 +    'Imágenes: ' + GetUserSpecialDir(5) + #13#10 +    'Escritorio: ' + GetUserSpecialDir(6)  );   // LLAMADA SIMPLE  ShowMessage('Tu carpeta de Descargas está en: ' + GetUserSpecialDir(2));end; procedure TForm1.FormCreate(Sender: TObject);begin end; end.   P.S.: I'm attaching the project and image. ---------------------------------------------------------------------- paule32 · 2025-03-31 06:39 Thank's for your efforts. is it possible to detect the underlaying system like: Windows, Linix, MinGW-32/64 (MSYS2) ? Under Windows, I can start MSYS Application, but they do use other directory convention's. So, the POSIX emulated Application's lay in: C:\MSYS64\ C:\MSYS64\bin\bash.exe but they are handled internaly as: /c/msys64/bin/bash  or (in the command shell): /bin/bash Thanks for feedback. ---------------------------------------------------------------------- Bart · 2025-03-31 09:46 For Windows, most of what you coded is already available in the WinDirs unit. Bart ---------------------------------------------------------------------- marcov · 2025-03-31 11:14 Quote from: paule32 on March 31, 2025, 08:39:53 amThank's for your efforts. is it possible to detect the underlaying system like: Windows, Linix, MinGW-32/64 (MSYS2) ? I don't know if MSYS can be detected(ask them) , but even if you can, FPC applications will remain Windows applications, and not posix emulation. I.e. they will call Windows directly, not via msys. If you want that, you need to make a new msys FPC target. ---------------------------------------------------------------------- paule32 · 2025-03-31 12:08 - you don't need explicit msys fpc target. - you can read-out the TERM environ variable   - but then, you have several problems:     - is it 32 or 64 bit     - is it Cygwin or MinGW     - is it Windows Command Line (TUI)     - is it for Windows 7, 8, 10, 11, 12 ?     - is it PowerShell (GUI)     - is it GUI or TUI - for the last, you can have a mixture of GUI and TUI ---------------------------------------------------------------------- marcov · 2025-03-31 12:56 Quote from: paule32 on March 31, 2025, 02:08:08 pm- you don't need explicit msys fpc target. What i meant is that all routines will still use windows path conventions. So you need to be very careful what paths to send to msys stuff, and what paths to send to direct Windows API. And the TERM environment variabele might be used for various unix emulation things (does Hummingbird Exceed still exist?). It is not cygwin or msys specific. ---------------------------------------------------------------------- d2010 · 2025-04-02 00:23 Not in fpc, in other type Code: [Select]Build   [Error] body.pas(3299): Undeclared identifier: 'CoTaskMemFree' I solved with , I add "ActiveX," before the Windows. Uses  hc_asiconst,     Batch,     ActiveX, //coTaskMemFree     windows; Thank you.