Easy question...

How can I retrieve the Window's Program Folder in Delphi?
MarcelitoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
rwilson032697Commented:
This is not a special windows folder. What do you want to do, exactly?

Cheers,

Raymond.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
intheCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.