Lazarus

Free Pascal => Beginners => Topic started by: Petrus Vorster on September 05, 2024, 10:29:24 am

Title: Desktop folder
Post by: Petrus Vorster on September 05, 2024, 10:29:24 am
Hi All

How do I get the Desktop folder in windows?
I need to drop the CSV file on the desktop for the user.
(preferably without the user action or selections.)

I have gone through a lot of searches on the forum, but there are a lot of answers that I currently know nothing off.

Thank you for all your help. It is much appreciated.

-Peter
Title: Re: Desktop folder
Post by: ASerge on September 05, 2024, 10:34:17 am
Code: Pascal  [Select][+][-]
  1. uses WinDirs;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. begin
  5.   Caption := GetWindowsSpecialDir(CSIDL_DESKTOPDIRECTORY);
  6. end;
  7.  
Title: Re: Desktop folder
Post by: Petrus Vorster on September 05, 2024, 10:35:44 am
That's it???

WOW.

Thanks mate.

-Peter
Title: Re: Desktop folder
Post by: Zvoni on September 05, 2024, 10:40:19 am
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Uses Sysutils;
  3. begin
  4.   writeln(GetUserDir);
  5. end.

.... and just append "\Desktop" to it

Note: This goes Kaboom in case of OneDrive (like on my company laptop. My "Desktop" is within OneDrive, so.....)
If it'S OneDrive-Based, you first have to check, if OneDrive is installed (in AppData IIRC), and then build the final Path-String
As a final Sanity-Check use "DirectoryExists" beofre dropping the File on the Desktop

EDIT: AAHA.... Thx Serge.
Didn't know that one.

EDIT2: YAY! Serge's Solution works even with OneDrive-based Folders
Title: Re: Desktop folder
Post by: eny on September 05, 2024, 09:10:21 pm
.... and just append "\Desktop" to it
This also does Kaboom for non-English versions of Window$  ;)
Title: Re: Desktop folder
Post by: Remy Lebeau on September 05, 2024, 09:24:45 pm
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Uses Sysutils;
  3. begin
  4.   writeln(GetUserDir);
  5. end.

.... and just append "\Desktop" to it

DO NOT do that.  That is not guaranteed to be the correct path on every version of every system.  Shell folders can be renamed (localization, etc), relocated by the user, mapped by the system (OneDrive, etc), and so on.  Windows knows the exact and correct path, so ask Windows where it actually is.  Same with ANY system-managed path.  NEVER create system paths manually.
Title: Re: Desktop folder
Post by: dbannon on September 06, 2024, 04:22:13 am
Hmm, thats interesting, I do this -

Code: Pascal  [Select][+][-]
  1.         {$ifdef WINDOWS}
  2.         TheHomeDir := appendPathDelim(GetEnvironmentVariableUTF8('HOMEPATH'));

And assume Desktop is immediately below that. Is that not a safe practice ?

Davo
Title: Re: Desktop folder
Post by: dsiders on September 06, 2024, 04:48:31 am
Hmm, thats interesting, I do this -

Code: Pascal  [Select][+][-]
  1.         {$ifdef WINDOWS}
  2.         TheHomeDir := appendPathDelim(GetEnvironmentVariableUTF8('HOMEPATH'));

And assume Desktop is immediately below that. Is that not a safe practice ?

Davo

I think you need the combined values of HOMEDRIVE and HOMEPATH on Windows.
Title: Re: Desktop folder
Post by: Zvoni on September 06, 2024, 08:23:45 am
Hmm, thats interesting, I do this -

Code: Pascal  [Select][+][-]
  1.         {$ifdef WINDOWS}
  2.         TheHomeDir := appendPathDelim(GetEnvironmentVariableUTF8('HOMEPATH'));

And assume Desktop is immediately below that. Is that not a safe practice ?

Davo
No it's not. Exactly because of the reasons Remy described.
On my company-laptop my "Desktop" is on a OneDrive, which is NOT directly below HOMEPATH
Title: Re: Desktop folder
Post by: dbannon on September 06, 2024, 02:05:39 pm
Hmm, OK, sounds like I had better fix that before next release. I have not had any complaints and about  2/3 of my users seem to be on Windows. But I can understand its wrong !

Thanks for thee heads up !

Davo
TinyPortal © 2005-2018