Easy question...

How can I retrieve the Window's Program Folder in Delphi?
MarcelitoAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
i believe your component is only a wrapper around SHGetSpecialFolderLocation() which does not return the "program files" location as has been saif it is bnnot a specialfolder or in the csidl_ consts.
i post this as answer because i believe it is probably most correct way to do it:

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;


0
 
intheCommented:
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
0
 
MarcelitoAuthor Commented:
Sorry, I need 'Programs Files' directory

Sorry again.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Cheers,

Raymond.
0
 
MadshiCommented:
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.
0
 
MarcelitoAuthor Commented:
My application search for a file, and this search must start in 'Program Files' directory. This directory can be different in each PC.
0
 
intheCommented:
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  :-)
0
 
MarcelitoAuthor Commented:
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...
0
 
DrDelphiCommented:
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!!  
0
 
intheCommented:
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.
0
 
haehnenCommented:
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
0
 
MadshiCommented:
Thanx, Barry, for the tip...   :-)
0
 
craig_capelCommented:
err, GetWindowsDirectory(lpBuffer, 255); should that not automaticly just do it, regardless of what language it is is in...

Craig C.


0
 
craig_capelCommented:
oops, got a bit of a stutter there :)
0
All Courses

From novice to tech pro — start learning today.