如何在C#中以管理员模式启动进程

时间:2010-03-28 11:54:16

标签: c# .net

我有一个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;

9 个答案:

答案 0 :(得分:74)

试试这个:

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

或者,go the manifest route for your application

答案 1 :(得分:23)

首先,您需要在项目中加入

using System.Diagnostics;

之后,您可以编写一个通用方法,您可以将其用于要使用的不同.exe文件。它将如下所示:

public void ExecuteAsAdmin(string fileName)
{
    Process proc = new Process();
    proc.StartInfo.FileName = fileName;
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.Verb = "runas";
    proc.Start();
}

如果您想执行notepad.exe,那么您只需调用此方法:

ExecuteAsAdmin("notepad.exe");

答案 2 :(得分:20)

这是您问题的明确答案: How do I force my .NET application to run as administrator?

要点:

右键点击项目 - >添加新项目 - >应用程序清单文件

然后在该文件中更改一行如下:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

编译并运行!

答案 3 :(得分:18)

var pass = new SecureString();
pass.AppendChar('s');
pass.AppendChar('e');
pass.AppendChar('c');
pass.AppendChar('r');
pass.AppendChar('e');
pass.AppendChar('t');
Process.Start("notepad", "admin", pass, "");

也适用于ProcessStartInfo

var psi = new ProcessStartInfo
{
    FileName = "notepad",
    UserName = "admin",
    Domain = "",
    Password = pass,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
Process.Start(psi);

答案 4 :(得分:18)

这在我尝试时有用。我用两个示例程序仔细检查了一下:

using System;
using System.Diagnostics;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      Process.Start("ConsoleApplication2.exe");
    }
  }
}

using System;
using System.IO;

namespace ConsoleApplication2 {
  class Program {
    static void Main(string[] args) {
      try {
        File.WriteAllText(@"c:\program files\test.txt", "hello world");
      }
      catch (Exception ex) {
        Console.WriteLine(ex.ToString());
        Console.ReadLine();
      }
    }
  }
}

首先证实我得到了UAC炸弹:

  

System.UnauthorizedAccessException的:   访问路径'c:\ program   files \ test.txt'被拒绝。
//   等。

然后使用短语:

向ConsoleApplication1添加了一个清单
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

没有炸弹。而且我无法轻易删除文件:)这与运行Vista和Win7的各种机器上的几个先前测试一致。启动的程序从启动程序继承安全令牌。如果启动器已获得管理员权限,则启动的程序也会获得管理员权限。

答案 5 :(得分:5)

以下是没有Windows提示符的管理员运行过程的示例

  Process p = new Process();
  p.StartInfo.FileName = Server.MapPath("process.exe");
  p.StartInfo.Arguments = "";
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.CreateNoWindow = true;
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.Verb = "runas";
  p.Start();
  p.WaitForExit();

答案 6 :(得分:0)

您可能需要将应用程序设置为x64应用程序。

IIS Snap In仅在64位工作,不能在32位工作,从32位应用程序生成的进程似乎是32位进程,64位应用程序也是如此。

请注意:Start process as 64 bit

答案 7 :(得分:0)

使用此方法:

public static int RunProcessAsAdmin(string exeName, string parameters)
    {
        try {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = CurrentDirectory;
            startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
            startInfo.Verb = "runas";
            //MLHIDE
            startInfo.Arguments = parameters;
            startInfo.ErrorDialog = true;

            Process process = System.Diagnostics.Process.Start(startInfo);
            process.WaitForExit();
            return process.ExitCode;
        } catch (Win32Exception ex) {
            WriteLog(ex);
            switch (ex.NativeErrorCode) {
                case 1223:
                    return ex.NativeErrorCode;
                default:
                    return ErrorReturnInteger;
            }

        } catch (Exception ex) {
            WriteLog(ex);
            return ErrorReturnInteger;
        }
    }

希望它有所帮助。

答案 8 :(得分:0)

 Process proc = new Process();                                                              
                   ProcessStartInfo info = 
                   new ProcessStartInfo("Your Process name".exe, "Arguments");
                   info.WindowStyle = ProcessWindowStyle.Hidden;
                   info.UseShellExecute =true;
                   info.Verb ="runas";
                   proc.StartInfo = info;
                   proc.Start();
相关问题