Board index » delphi » Getting Windows Desktop Folder?

Getting Windows Desktop Folder?

Does anyone know how to retrieve the location of the desktop folder?
I.e., if someone saves question.txt on the desktop, the actual patch
of that file could be
C:\Windows\Desktop\question.txt
C:\WinNT\Profiles\MyName\Desktop\question.txt
etc.

I figure there's some sort of API call for this, but I couldn't find
it...any ideas?

 

Re:Getting Windows Desktop Folder?


Daryl,

I have got a nice function to retrieve the desktop folder. Here it comes:

function DesktopFolder: string;
var
  buf: array[0..MAX_PATH] of char;
  pidList: PItemIDList;
begin
  Result := '';
  SHGetSpecialFolderLocation(Application.Handle, CSIDL_DESKTOP, pidList);
  if pidList = nil then
    exit; // no Desktop? Want to see that computer...
  if SHGetPathFromIDList(pidList, buf) then
    Result := buf;
end;

Sincerely,

Fred Jansma.

Quote
Daryl Smith wrote in message <35eafaec.12614...@plnews.pl.unisys.com>...

>Does anyone know how to retrieve the location of the desktop folder?
>I.e., if someone saves question.txt on the desktop, the actual patch
>of that file could be
>C:\Windows\Desktop\question.txt
>C:\WinNT\Profiles\MyName\Desktop\question.txt
>etc.

>I figure there's some sort of API call for this, but I couldn't find
>it...any ideas?

Re:Getting Windows Desktop Folder?


Quote
In article <35eafaec.12614...@plnews.pl.unisys.com>, daryl.smi...@unisys.com (Daryl Smith) wrote:

>Does anyone know how to retrieve the location of the desktop folder?
>I.e., if someone saves question.txt on the desktop, the actual patch
>of that file could be
>C:\Windows\Desktop\question.txt
>C:\WinNT\Profiles\MyName\Desktop\question.txt
>etc.

>I figure there's some sort of API call for this, but I couldn't find
>it...any ideas?

Since I've already written the code for just that purpose, I'll just
give it here:

unit BHUtils;
(* General utility procedures *)

{$EXTENDEDSYNTAX ON}

interface

uses
  Windows, SysUtils, Registry;

(*
  Return the current location of one of the standard folders.
  Examples include 'Temp', 'Start Menu', 'Programs', 'Startup',
  'Desktop', 'Personal', 'Favorites', 'Fonts', 'SendTo', 'Recent',
  'NetHood', 'PrintHood', 'Templates', 'AppData'.
*)
function StandardFolder(const Which: string): string;

implementation

function StandardFolder(const Which: string): string;
const
  RegFolders =
    '\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders';
var R: TRegistry; Buffer: array[0..1023] of Char;
begin
  Result := ''; (* Default return value *)
  if AnsiCompareText(Which, 'TEMP') = 0 then
    (* Get TEMP directory *)
    SetString(Result, Buffer, GetTempPath(Sizeof(Buffer) - 1, Buffer))
  else begin
    (* Get User folder *)
    R := TRegistry.Create;
    try
      if R.OpenKey(RegFolders, False) then
        try
          Result := R.ReadString(Which)
        except (* Result = '' *)
        end
    finally
      R.Free
    end
  end
end;

end.

brian.haw...@bigfoot.com

Re:Getting Windows Desktop Folder?


Thanks for the tips...I appreciate it!

Re:Getting Windows Desktop Folder?


Brian,

check out my reply. A function like this was my first attempt as well, but a
friend of mine (fellow Delphi programmer) pointed out to me, that HIS code
was better. In the source I send you are not so much dependant of the place
MS puts it in with the next version of IE or WIN.

Sincerely,

Fred Jansma.

Re:Getting Windows Desktop Folder?


Example:

uses ....,registry,.....

procedure TForm1.Button1Click(Sender: TObject);
var
 theReg  : TRegistry;
 KeyName : String;
begin
 theReg := TRegistry.Create;
 KeyName := 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders';
 if (theReg.KeyExists(KeyName)) then
   begin
     theReg.OpenKey(KeyName, False);
     label1.caption := theReg.ReadString('Desktop');
   end
 else
   begin
     theReg.OpenKey(KeyName, True);
     Label1.caption := 'Error';
   end;
 theReg.Free;

end;

Hope this help?

                        Zdravko Dokuzovic
                        Slavonski Brod, Croatia
                        e-mail: zdoku...@barok.foi.hr

Quote
Daryl Smith wrote in message <35eafaec.12614...@plnews.pl.unisys.com>...

>Does anyone know how to retrieve the location of the desktop folder?
>I.e., if someone saves question.txt on the desktop, the actual patch
>of that file could be
>C:\Windows\Desktop\question.txt
>C:\WinNT\Profiles\MyName\Desktop\question.txt
>etc.

>I figure there's some sort of API call for this, but I couldn't find
>it...any ideas?

Re:Getting Windows Desktop Folder?


Quote
Daryl Smith wrote in message <35eafaec.12614...@plnews.pl.unisys.com>...

>Does anyone know how to retrieve the location of the desktop folder?
>I.e., if someone saves question.txt on the desktop, the actual patch
>of that file could be
>C:\Windows\Desktop\question.txt
>C:\WinNT\Profiles\MyName\Desktop\question.txt

---
First call "SHGetSpecialFolderLocation" to get a PIDL to the desktop (use
CSIDL_DESKTOP as a parameter) and then "SHGetPathFromIDList" to convert the
PIDL to a path. You'll find more info in the Win32.hlp file.

Regards,
Theodor

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Theodor Kleynhans  theod...@iafrica.com

For some great GIF support for Delphi
and/or GIF support for your favourite
Pegasus Imaging tool, drop on by

  http://members.gem.co.za/~theodor/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Other Threads