BCS Codesnips Launch Windows Explorer To Specific Folder

There are those occasions when the windows explorer is needed at a specific location.  The following code may be employed to achieve the successful launch programatically.
[codesyntax lang=”delphi” title=”Launch Windows Explorer Targeting Specific Folder”]

procedure TBCSBuildStackC.XQTExplorer(ipath: AnsiString);
begin
  WinExec(PAnsiChar(Self.sys + '\explorer.exe /Select,"' + ipath + '"'),
    sw_Normal);
end;

[/codesyntax]
The sys variable is populated by retrieving the system variable that points to the windows system root.
The ipath variable is the folder location where the explorer should open when launched.  It may be populated any number of ways.  Typically a open file dialog is employed for that purpose.
The overall result is to launch the windows explorer to that specific folder from a executing Delphi program.
Mr. Arch Brooks, Software Engineer

2 comments
    • Maya
      Maya
      February 12, 2011 at 9:52 pm

      Ah, I see what you’re having a problem with now. Having to specify the windows program to open the file/folder with. I agree, you probably don’t want to do that, because a windows update may break your app.
      Have a look at this link:
      http://bcbjournal.org/articles/vol3/9903/Spawning_external_applications.htm
      And take spacial note of this part:
      Now, let’s look at an example that opens a particular document file. Look at these two lines of code:
      ShellExecute(Handle, “open”, “test.txt”, 0, 0, SW_NORMAL);
      ShellExecute(Handle, 0, “notepad.exe”, “test.txt”, 0, SW_NORMAL);
      In the first example, we simply pass a command of open and the filename to open. Windows uses file association to open the application associated with the TXT extension (Notepad in most cases). In the second example, we pass the name of the executable in the lpFile parameter and the name of the file to open in the lpParameters parameter. Use the latter method if you prefer not to rely on Windows file associations to run an application. These two lines result in exactly the same behavior if Windows Notepad is associated with the TXT extension (the default Windows setup).
      PS. It is talking about C++ builder but the Delphi functions are absolutely indentical here.

      Reply
Leave a Reply

Your email address will not be published. Required fields are marked *