The Unofficial Newsletter of Delphi Users - by Robert Vivrette




You Can Never Have Too Much WinInfo!

by Peter Lennard Larsen - s01pll@msektor.auc.dk

Do you need to find out where a users "Program Files" directory is? You could assume that it was off of the root, but remember, the operating system allows you to move it! The same holds true for most (if not all) of the other "special" directories managed by Windows. This simple component allows you to always have this information at the tip of your fingers. Simply include the unit in your Uses clause. Then, when you need to know the location of one of these special directories, you simply call the appropriate property from the WindowsInfo object this unit creates. For example, the location of the desktop is WindowsInfo.DesktopDir. Simple!

Note also that this unit works well in conjunction with my other article on Creating Shortcuts. And there is also a sample project that illustrates the use of both of these techniques. You can download this demo by clicking here (for my Delphi 2 demo) or by clicking here (for Robert Vivrette's Delphi 3 demo).

unit wininfo;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Registry;

type
  TWindowsInfo = class
  private
    { Private declarations }
      FDesktopDir: string;
      FFavoritesDir: string;
      FFontsDir: string;
      FNetHoodDir: string;
      FPersonalDir: string;
      FProgramsDir: string;
      FRecentDir: string;
      FSendToDir: string;
      FStartMenuDir: string;
      FStartupDir: string;
      FTemplatesDir: string;
  protected
    { Protected declarations }
  public
    { Public declarations }
      constructor Create;
  published
    { Published declarations }
      property DesktopDir: string read FDesktopDir;
      property FavoritesDir: string read FFavoritesDir;
      property FontsDir: string read FFontsDir;
      property NetHoodDir: string read FNetHoodDir;
      property PersonalDir: string read FPersonalDir;
      property ProgramsDir: string read FProgramsDir;
      property RecentDir: string read FRecentDir;
      property SendToDir: string read FSendToDir;
      property StartMenuDir: string read FStartMenuDir;
      property StartupDir: string read FStartupDir;
      property TemplatesDir: string read FTemplatesDir;
  end;

var
   WindowsInfo: TWindowsInfo;

implementation

constructor TWindowsInfo.Create;
const
  WinKey = '\Software\Microsoft\Windows\CurrentVersion\';
  ExplorerKey = '\Software\Microsoft\Windows\CurrentVersion\Explorer\';
var
   Reg: TRegistry;
begin
   inherited Create;
   Reg := TRegistry.Create;
   try
      Reg.RootKey := HKey_Current_User;
      if Reg.OpenKey(ExplorerKey+'Shell Folders', FALSE) then begin
         FDesktopDir:=Reg.ReadString('Desktop');
         FFavoritesDir:=Reg.ReadString('Favorites');
         FFontsDir:=Reg.ReadString('Fonts');
         FNetHoodDir:=Reg.ReadString('NetHood');
         FPersonalDir:=Reg.ReadString('Personal');
         FProgramsDir:=Reg.ReadString('Programs');
         FRecentDir:=Reg.ReadString('Recent');
         FSendToDir:=Reg.ReadString('SendTo');
         FStartMenuDir:=Reg.ReadString('Start Menu');
         FStartupDir:=Reg.ReadString('Startup');
         FTemplatesDir:=Reg.ReadString('Templates');
      end;
   finally
     Reg.Free;
   end;
end;

initializiation
   WindowsInfo := TWindowsInfo.Create;

finalization
   WindowsInfo.Free;

end.