Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Windows file Explorer at a specified directory?

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi All

What's the best way to fire off file explorer at a given directory? I've really hit a mental block on this one and would appreciate some help.

Many thanks in advance for help
lou
 
It's ok, found this:


uses
ShellApi;

// strFolder is the folder you want to open
procedure ShowFolder(strFolder: string);
begin
ShellExecute(Application.Handle,
PChar('explore'),
PChar(strFolder),
nil,
nil,
SW_SHOWNORMAL);
end;

procedure TForm1.Button1Click(Sender: TObject);
const
strFolder = 'c:\My Documents';
begin
ShowFolder(strFolder);
end;
 
Glad we could help! ;-)

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I shouldn't have opened my mouth!!!

Is it opening Windows Explorer? If so, what directory is being shown?

I've hard-coded the following code into a button onclick event and it seems to work fine (Delphi 6, Win XP):
Code:
ShellExecute(Application.Handle,
               PChar('explore'),
               PChar('c:\temp'),
               nil,
               nil,
               SW_SHOWNORMAL);

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I may be wrong, but your sample code suggests you are attempting to open the directory 'c:\My Documents' which in all likelihood does not actually exist. The My Documents folder is normally found in the following path: 'C:\Documents and Settings\UserName\My Documents'.

Here is some sample code I adapted from which opens up the My Documents folder with no problem. I'm sure I've come across an easier way to do this in the past but I can't seem to find any hint of it.
Code:
uses
  ShellApi, Registry;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(Application.Handle,
               PChar('explore'),
               PChar(MyDocumentsDirectory),
               nil,
               nil,
               SW_SHOWNORMAL);
end;

function  GetRegistryKeyValue(ARoot: HKEY; AKey,AName: string) : string;
var
  Reg: TRegistry;
begin
  Result := '';
  Reg := TRegistry.Create;
  try
    Reg.RootKey := ARoot;
    If Reg.OpenKey(AKey, FALSE) then
      Result := Reg.ReadString(AName);
  finally
    Reg.Free;
  end;
end;

function TForm1.MyDocumentsDirectory: String;
begin
  Result := IncludeTrailingPathDelimiter(GetRegistryKeyValue(HKEY_CURRENT_USER,
                                         'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
                                         'Personal'));
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
hi Clive

I've fixed it, really stupid. I was trying to open a non existent directory because I had a 'not' in my 'if not directoryexists' [blush]

Sorry for wasting your time and many many thanks for taking the time to answer.

lou
 
No problem lou!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top