以管理员c#的其他用户身份启动流程

时间:2013-09-13 12:52:40

标签: c# windows-8 windows-7

我需要以管理员身份安装。由于无法授予用户管理员权限,因此我需要指定管理员帐户。我目前的代码:

var info = new ProcessStartInfo(pathToSomeFileMsiFile.msi)
            {
                Arguments = " /q "
            };
info.UserName = "usernamer";
info.Domain = "dm";
info.Password = securePasswordString;
info.LoadUserProfile = false;
info.UseShellExecute = false;
info.Verb = "runas";
Process.Start(info);

我收到错误:

{"The specified executable is not a valid application for this OS platform."}

似乎如果我设置UseShellExecute = true,我可以以管理员身份启动,如果我将其设置为false,我可以以其他用户身份启动。

任何帮助?

2 个答案:

答案 0 :(得分:2)

您需要使用command line parameter msi文件运行msiexec.exe

msiexec.exe /i /q somepackage.msi

所以你的更新代码是:

var info = new ProcessStartInfo(@"C:\windows\system32\msiexec.exe")
        {
            Arguments = " /i " + varableWithSomeMSIFilename + " /q"
        };

答案 1 :(得分:0)

设置:info.UseShellExecute = true;

一个例子:

    ProcessStartInfo proc = new ProcessStartInfo();

  proc.UserName = "usernamer";
proc.Domain = "dm";
proc.Password = securePasswordString;
proc.LoadUserProfile = false;

    proc.UseShellExecute = false;
    proc.WorkingDirectory = Environment.CurrentDirectory;
    proc.FileName = Application.ExecutablePath;
    proc.Verb = "runas";

            try
            {
                Process.Start(proc);
            }
            catch

            {
                // The user refused the elevation.
                // Do nothing and return directly ...
                return;
            }

            Application.Exit();  // Quit itself