Recent

Author Topic: [Solved] Get user pictures directory name in Windows and Linux  (Read 3512 times)

artem101

  • Jr. Member
  • **
  • Posts: 84
[Solved] Get user pictures directory name in Windows and Linux
« on: September 10, 2022, 10:21:15 am »
What is cross-platform way to get user pictures dir on Windows and Linux? Like "C:\Documents and Settings\user\My Documents\My Pictures" in Windows XP, "C:\Users\user\Pictures" in Windows 7 and later, "/home/user/Pictures" in Linux.
« Last Edit: September 11, 2022, 10:34:28 pm by artem101 »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6763
Re: Get user pictures directory name in Windows and Linux
« Reply #1 on: September 10, 2022, 10:53:54 am »
I can't speak for Windows, but in the case of Linux you're entirely at the mercy of the selected distro and the window manager (desktop environment etc.) that you're currently running.

Refer to xdg-user-dir and related xdg utilities as shown by the apropos command, but note that support might be patchy and might be completely missing on a system set up as a server.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Get user pictures directory name in Windows and Linux
« Reply #2 on: September 10, 2022, 11:17:55 am »
for windows:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ... shfolder ....
  3.  
  4. ...
  5.  
  6. function GetSpecialFolder(const CSIDL: Integer): string;
  7. var
  8.   i: Integer;
  9. begin
  10.  SetLength(Result, MAX_PATH);
  11.  ShGetFolderPath(0, CSIDL, 0, 0, PChar(Result));
  12.  i := Pos(#0, Result);
  13.  if (i > 0) then
  14.    SetLength(Result, pred(i));
  15. end;
  16.  
  17. ...
  18.  
  19. var
  20.   s: string;
  21. begin
  22.  s := GetSpecialFolder(CSIDL_MYPICTURES);
  23.  
  24. ...
  25.  
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Josh

  • Hero Member
  • *****
  • Posts: 1305
Re: Get user pictures directory name in Windows and Linux
« Reply #3 on: September 10, 2022, 11:25:11 am »
windows only

uses windirs;

var s:unicodestring;
begin
  s:=GetWindowsSpecialDirUnicode(CSIDL_MYPICTURES);
end;     
« Last Edit: September 10, 2022, 11:54:50 am by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Kays

  • Hero Member
  • *****
  • Posts: 586
  • Whasup!?
    • KaiBurghardt.de
Re: Get user pictures directory name in Windows and Linux
« Reply #4 on: September 10, 2022, 02:13:56 pm »
Are there any real-life users out there who actually used the pre-created directories “My Pictures” etc. in their intended way? It was the first thing I deleted.
What is cross-platform way to get user pictures dir on Windows and Linux? […]
Frankly, I would treat the notion of “special” directories as nonexistent. Only that is cross-platform.
[…] Refer to xdg-user-dir and related xdg utilities as shown by the apropos command, but note that support might be patchy and might be completely missing on a system set up as a server. […]
The XDG utilities are at least mentioned in the Linux Standard Base. However, xdg‑user‑dir(1) is not a mandatory utility, therefore it may be missing on the specific machine. Not worth mentioning, there are/were hardly any distros fully supporting the LSB, so your statement “at the mercy of the selected distro” is quite accurate.
« Last Edit: September 10, 2022, 02:17:23 pm by Kays »
Yours Sincerely
Kai Burghardt

dbannon

  • Hero Member
  • *****
  • Posts: 2854
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Get user pictures directory name in Windows and Linux
« Reply #5 on: September 10, 2022, 02:14:55 pm »
....Linux you're entirely at the mercy of the selected distro and the window manager (desktop environment etc.) that you're currently running.

While Mark is definitely right, there is no certain answer. But a pretty good guess on a desktop system is ~/Pictures - don't assume its there, always check, but its there in all my VBVMs including Debian (and Ubuntu, Mint, MXLinux), Fedora, Manjaro. The major Desktops seem to agree ...

Maybe someone can confirm Arch and SUSE.

~/ refers to user home directory, get that from the $HOME env var.

Not certain, but a good place to start. Mind you, its Linux we are talking about, just because the directory exits, the user may like to put their pictures somewhere else.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

PascalDragon

  • Hero Member
  • *****
  • Posts: 5526
  • Compiler Developer
Re: Get user pictures directory name in Windows and Linux
« Reply #6 on: September 11, 2022, 05:34:49 pm »
Are there any real-life users out there who actually used the pre-created directories “My Pictures” etc. in their intended way? It was the first thing I deleted.

I do. And I either use them as is or redirect them (mainly the Downloads directory). The later also updates what the API functions return.

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: Get user pictures directory name in Windows and Linux
« Reply #7 on: September 11, 2022, 10:34:14 pm »
Thanks all. I need this for set default directory for screen capture program. So no need to run it on servers without GUI. I think using GetWindowsSpecialDirUnicode(CSIDL_MYPICTURES) + ~/Pictures enough for my case.

 

TinyPortal © 2005-2018