2010. július 17., szombat

Set the Desktop as the initial directory


Problem/Question/Abstract:

In a TOpenDialog, you can set the initial directory. How do I set it as the desktop? I could set it as c:\windows\desktop, but then what if Windows is not on the user's c drive?

Answer:

There is a shell function that can be used to inquire about the location of several shell-related folders. You need to add ShlObj to the uses clause for this, plus ActiveX for the CoTaskMemFree.

procedure FreePidl(pidl: PItemIDList);
begin
  CoTaskMemFree(pidl);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  pidl: PItemIDList;
  buf: array[0..MAX_PATH] of Char;
begin
  if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_DESKTOP, pidl)) then
  begin
    if ShGetPathfromIDList(pidl, buf) then
      ShowMessage(buf);
    FreePIDL(pidl);
  end;
end;

See win32.hlp (or better msdn.microsoft.com, the list has been extended for Win98/2000) for a list of CSIDL values. There is also a newer ShGetSpecialFolderPath API that directly returns the path, but it is not available on older Win95 and NT installations.

Nincsenek megjegyzések:

Megjegyzés küldése