使用shell执行以管理员身份运行cmd [英] use shell execute to run cmd as Admin

查看:995
本文介绍了使用shell执行以管理员身份运行cmd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个按钮上单击运行cmd。有用。但我需要作为管理员来运行它。这样做如何?

  ShellExecute(Handle,'open','c:\Windows\system32\cmd。 exe',nil,nil,SW_SHOWNORMAL)


解决方案

打开动词与 runas 如下所示。无论如何,尽量避免路径硬编码:

 使用
ShellAPI;

procedure TForm1.Button1Click(Sender:TObject);
begin
ShellExecute(Handle,'runas','cmd.exe',nil,nil,SW_SHOWNORMAL);
结束

您还可以通过设置 ElevationRequired 属性为True。 / p>

I need to run cmd on a button click as admin. It works. but I need to run it as an admin. How is this done?

 ShellExecute(Handle, 'open', 'c:\Windows\system32\cmd.exe', nil, nil, SW_SHOWNORMAL)

解决方案

Replace the open verb with the runas as shown below. Anyway, try to avoid path hardcoding:

uses
  ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(Handle, 'runas', 'cmd.exe', nil, nil, SW_SHOWNORMAL);
end;

You can also add to your button the shield icon by setting the ElevationRequired property to True.

这篇关于使用shell执行以管理员身份运行cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆