如何在 C# 中以管理员模式启动进程 [英] How to start a Process as administrator mode in C#

查看:34
本文介绍了如何在 C# 中以管理员模式启动进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Visual Studio Windows 应用项目.我添加了代码来下载安装程序更新文件.下载完成后的安装程序需要管理员权限才能运行.我添加了一个清单文件.

当用户单击 DownloadUpdate.exe 时,UAC 会提示用户输入管理员权限.所以我假设在 DownloadUpdate.exe 中创建和调用的所有进程都将以管理员身份运行.所以我使用以下代码使安装程序调用我下载的文件:

Process p = new Process();p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;p.StartInfo.FileName = strFile;p.StartInfo.UseShellExecute = false;p.StartInfo.RedirectStandardOutput = true;p.StartInfo.RedirectStandardError = true;

解决方案

试试这个:

//Vista或更高版本检查if (System.Environment.OSVersion.Version.Major >= 6){p.StartInfo.Verb = "runas";}

或者,为您的应用程序使用清单路径.>

I have a Visual Studio Windows app project. I've added code to download an installer update file. The installer after it has finished downloading would need administrator privileges to run. I have added a manifest file.

When user clicks on the DownloadUpdate.exe, UAC prompts the user for Admin permissions. So I assumed that all processes created and called within DownloadUpdate.exe will run in admin capacity. So I made the setup call my downloaded file with the following code:

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = strFile;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

解决方案

Try this:

//Vista or higher check
if (System.Environment.OSVersion.Version.Major >= 6)
{
   p.StartInfo.Verb = "runas";
}

Alternatively, go the manifest route for your application.

这篇关于如何在 C# 中以管理员模式启动进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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