Link to home
Start Free TrialLog in
Avatar of Marcelito
Marcelito

asked on

Easy question...

How can I retrieve the Window's Program Folder in Delphi?
Avatar of inthe
inthe

Hi like this

procedure TForm1.Button1Click(Sender: TObject);
var
lpBuffer: array[0..255] of char;
SysDir: string;
begin
GetWindowsDirectory(lpBuffer, 255);
SysDir := lpBuffer;
Label1.Caption := sysdir;
end;

Regards Barry
Avatar of Marcelito

ASKER

Sorry, I need 'Programs Files' directory

Sorry again.
This is not a special windows folder. What do you want to do, exactly?

Cheers,

Raymond.
For win2000 there is a new API called SHGetSpecialPath (I think that was the name) with that you can ask the program files directory.
In the "old" windows versions there is no API that, at least I don't know any...   :-(

Regards, Madshi.
My application search for a file, and this search must start in 'Program Files' directory. This directory can be different in each PC.
Hi
ahh but we can do this:


procedure TForm1.Button1Click(Sender: TObject);
var
drive : string;
I : integer;
Rslt: Integer;
SearchRec: TSearchRec;
root : string;
finished : boolean;
begin
finished:= false;
drive := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //maybe take out "A" drive
while not finished do begin
for i := 0 to 24 do begin
root := drive[i]+':';
 if Root[Length(Root)] = '\' then Root := Root + '*.*'
else Root := Root + '\*.*';
Rslt := FindFirst(Root, faDirectory, SearchRec);
if Rslt = 0 then
  begin
  while Rslt = 0 do
    begin
    if (SearchRec.Attr and faDirectory) <> 0 then
      begin
      if Pos('Program Files',searchrec.Name) <> 0
      then begin
       ShowMessage(drive[i]+':\'+SearchRec.Name); //have path here
       finished := true;
       end;
      end;
    Rslt := FindNext(SearchRec);
    end;
  FindClose(SearchRec);
  end;
end;
end;
end;

Regards Barry  :-)
Sorry, but if I install Windows in englis, the directory is 'Programs Files' and if I install in Spanish is 'Archivos de Programa', etc...
I need a function like GetWindowsDirectory or GetWindowsSystemDirectory...
Program Files is not a special directory. So far as I know it is not even on the path by default, which means you will have to look for it yourself. Barry's suggestion is probably the best you are going to see. I'd only suggest that if you know that you are distributing to different countries with other languages, you make allowances in your code.


Good luck!!  
ok the last way i think of:
note there are 2 reg keys where you can read this.


uses Registry;

procedure TForm1.Button1Click(Sender: TObject);
var
R: Tregistry;
begin
R :=TRegistry.Create;
R.Rootkey:=HKEY_LOCAL_MACHINE;
 with R do try
CreateKey('\Software\Microsoft\Windows\CurrentVersion\');
OpenKey('\Software\Microsoft\Windows\CurrentVersion\', True);
Edit1.Text := ReadString('ProgramFilesDir');
Edit2.Text := ReadString('ProgramFilesPath');
 R.CloseKey;
finally
  R.Free;
   end;
   end;

this should return path in corrct language.
Hi,
go to the http://www.torry.ru -> Components Page -> Files & Drives #2 -> TGetFoler (by haehnen ;c))
I think this component will help you
bye
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx, Barry, for the tip...   :-)
err, GetWindowsDirectory(lpBuffer, 255); should that not automaticly just do it, regardless of what language it is is in...

Craig C.


oops, got a bit of a stutter there :)