单击按钮时如何打开 Windows 资源管理器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:58:13  来源:igfitidea点击:

How to open windows explorer when I click a button?

windowsdelphiexplorer

提问by Tobassum Munir

I have a form in a Delphi project. There is a button on the form. When the user clicks the button, I want it to open Windows Explorer.

我在 Delphi 项目中有一个表单。表格上有一个按钮。当用户单击按钮时,我希望它打开 Windows 资源管理器。

What code will I need to achieve this?

我需要什么代码来实现这一目标?

回答by zz1433

Well in case you need to select some particular file in explorer I have the following function which I use

好吧,如果您需要在资源管理器中选择某个特定文件,我有以下功能可以使用

procedure SelectFileInExplorer(const Fn: string);
begin
  ShellExecute(Application.Handle, 'open', 'explorer.exe',
    PChar('/select,"' + Fn+'"'), nil, SW_NORMAL);
end;

and you can call it :

你可以称之为:

SelectFileInExplorer('C:\Windows\notepad.exe');

EDIT: As mentioned ShellAPI must be added to your uses list

编辑:如前所述,必须将 ShellAPI 添加到您的使用列表中

回答by JosephStyons

Building on what Mason Wheeler said: you can also pass in a directory as an argument, to get the window to open to a non-default location:

建立在梅森惠勒所说的基础上:您还可以传入一个目录作为参数,以使窗口打开到非默认位置:

uses
  ShellAPI;

...

  ShellExecute(Application.Handle,
    nil,
    'explorer.exe',
    PChar('c:\'), //wherever you want the window to open to
    nil,
    SW_NORMAL     //see other possibilities by ctrl+clicking on SW_NORMAL
    );

回答by Mason Wheeler

Try this:

尝试这个:

ShellExecute(Application.Handle, nil, 'explorer.exe', nil, nil, SW_NORMAL);

You'll need to add ShellAPIto your usesclause.

您需要添加ShellAPI到您的使用条款。

回答by mjn

According to http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx, ShellExecute also supports the 'explore' verb, which 'explores' a folder specified by lpFile, so this should work:

根据http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx,ShellExecute 还支持“探索”动词,它“探索”由 lpFile 指定的文件夹,所以这应该工作:

ShellExecute(Application.Handle, 'explore', '.', nil, nil, SW_NORMAL);