Recent

Author Topic: Desktop folder  (Read 719 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Desktop folder
« 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

ASerge

  • Hero Member
  • *****
  • Posts: 2320
Re: Desktop folder
« Reply #1 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.  

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Desktop folder
« Reply #2 on: September 05, 2024, 10:35:44 am »
That's it???

WOW.

Thanks mate.

-Peter

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
Re: Desktop folder
« Reply #3 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
« Last Edit: September 05, 2024, 10:42:00 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

eny

  • Hero Member
  • *****
  • Posts: 1644
Re: Desktop folder
« Reply #4 on: September 05, 2024, 09:10:21 pm »
.... and just append "\Desktop" to it
This also does Kaboom for non-English versions of Window$  ;)
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: Desktop folder
« Reply #5 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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

dbannon

  • Hero Member
  • *****
  • Posts: 3061
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Desktop folder
« Reply #6 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
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

dsiders

  • Hero Member
  • *****
  • Posts: 1238
Re: Desktop folder
« Reply #7 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.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
Re: Desktop folder
« Reply #8 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dbannon

  • Hero Member
  • *****
  • Posts: 3061
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Desktop folder
« Reply #9 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
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

 

TinyPortal © 2005-2018