在 vista 中以管理员身份以编程方式运行 cmd.exe,C#

发布于 2024-07-15 08:16:56 字数 465 浏览 12 评论 0原文

我有一个视觉工作室设置和部署项目。 我在其中添加了一个 .cmd 脚本。 该脚本需要管理员权限才能运行。 当用户单击 setup.exe 时,UAC 会提示用户授予管理员权限。 因此,我假设在 setup.exe 中创建和调用的所有进程都将以管理员身份运行。 因此,我进行了设置调用我的控制台应用程序,其中包含以下代码。

ProcessStartInfo p1 = new ProcessStartInfo();
p1.UseShellExecute = true;
p1.Verb = "runas";
p1.FileName = "cmd.exe";
Process.Start(p1);

所以它应该可以工作,因为它是在管理员空间下运行的。

我想以管理员身份通过 C# 进程类运行 cmd.exe。我正在运行 Windows Vista。

我试过 没用! 我能做些什么?

I have a visual studio setup and deployment project. I've added a .cmd script in it. The script would need administrator privileges to run. When user clicks on the setup.exe, UAC prompts the user for Admin permissions. So I assumed that all processes created and called within setup.exe will run in admin capacity. So I made the setup call my console application which contains the following code.

ProcessStartInfo p1 = new ProcessStartInfo();
p1.UseShellExecute = true;
p1.Verb = "runas";
p1.FileName = "cmd.exe";
Process.Start(p1);

So it should've worked as it's run under administrator space.

I want to run cmd.exe through c# process class as an administrator.I'm running windows vista.

I tried
didn't work! What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

|煩躁 2024-07-22 08:16:56

尝试执行 runas 命令:(

...

using System.Diagnostics;

...

string UserName = "user name goes here";
ProcessStartInfo p1 = new ProcessStartInfo();
  p1.FileName = "runas";
  p1.Arguments = String.Format("/env /u:{0} cmd", UserName);
Process.Start(p1);

...

而且我认为您不需要显式的 UseShellExecute)

Try executing the runas command:

...

using System.Diagnostics;

...

string UserName = "user name goes here";
ProcessStartInfo p1 = new ProcessStartInfo();
  p1.FileName = "runas";
  p1.Arguments = String.Format("/env /u:{0} cmd", UserName);
Process.Start(p1);

...

(And I don't think you need an explicit UseShellExecute)

赠佳期 2024-07-22 08:16:56

试试这个,这对我有用。

...

using System.Diagnostics;

...

ProcessStartInfo startInfo = new ProcessStartInfo();
  startInfo.UseShellExecute = true;            
  startInfo.Verb = "runas";
  startInfo.Arguments = "/env /user:" + "Administrator" + " cmd";
Process.Start(startInfo);

...

阿舒托什

Just Try this, This worked for Me.

...

using System.Diagnostics;

...

ProcessStartInfo startInfo = new ProcessStartInfo();
  startInfo.UseShellExecute = true;            
  startInfo.Verb = "runas";
  startInfo.Arguments = "/env /user:" + "Administrator" + " cmd";
Process.Start(startInfo);

...

Ashutosh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文