以管理员身份静默安装MSI

时间:2020-04-02 06:35:50

标签: c# .net winforms windows-installer

我正在尝试创建一个将从c#Windows应用程序安装msi的应用程序,在这里我想从用户那里输入用户名,域和密码,以便我可以该用户帐户运行该应用程序。在下面的代码中,如果我仅给出 startInfo.Verb =“ runas” 的信息,但是我想提供admin的用户名和密码并运行它。你们可以帮我吗?

private void InstallProbe()
{
    try
    {
      bool gdfg=  IsRunAsAdmin();
        //processObj.InitializeProcess(txtUserName.Text, txtPassword.Text);
        string installcmd = "/c msiexec /i \"{0}\" /quiet TARGETDIR=\"{1}\" HOST=\"{2}\" PORT=\"{3}\" USEHTTPS=\"{4}\"  STEALTHMODE=\"{5}\"  ORGCODE=\"{6}\"";
        installcmd = string.Format(installcmd, txtFilePath.Text, @"%PROGRAMFILES%\ProHance Mate", "services.jamochatech.com", "8080", false, 0, "PHSALE");
        string uname, domain = string.Empty;
        //RunCommand("cmd", installcmd, processObj);
        if (txtUserName.Text.IndexOf("\\") > 0)
        {
            string[] strarr = txtUserName.Text.Split('\\');
            uname = strarr[1];
            domain = strarr[0];
        }
        else
        {
            uname = txtUserName.Text;
            domain = ".";
        }

        Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        //startInfo.Verb = "runas";

        startInfo.Domain = domain;
        startInfo.UserName = uname;
        startInfo.Password = ToSecureString(txtPassword.Text);
        startInfo.FileName = "cmd";
        startInfo.Arguments = installcmd;
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.LoadUserProfile = true;
        MessageBox.Show(installcmd);
        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit(60000);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception occured while installing the ProHance Mate " + ex.Message);
    }
}

1 个答案:

答案 0 :(得分:0)

不管MSI上下文如何,您都只是在尝试在特定用户上下文下启动新进程(msiexec.exe)。检查下面的线程以及其他线程。

In Windows: How do you programatically launch a process in administrator mode under another user context?