使用C#静默安装应用程序时跳过向导UI

问题描述 投票:0回答:1

我正在研究C#控制台应用程序。它只有一个要求。它应该静默安装.exe安装文件。这些安装文件是从InstallShield或其他产品生成的。我没有生成任何设置文件。我尝试使用此代码安装Notepad ++,但它并不是真的。

为了更具体,并避免重复标记这个问题,我已经提到了很多相关的链接。他们之中有一些是

  1. http://www.c-sharpcorner.com/article/silent-installation-of-applications-using-c-sharp/
  2. C# code to run my installer.exe file in silent mode, in the background,
  3. How to run silent installer in C#

我试过的代码:

选项1

ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = newRenamedFile;
psi.UseShellExecute = false;
Process.Start(psi);

OPTION-2

executableFilePath是.exe安装文件的路径。

public static void DeployApplications(string executableFilePath)
{
    PowerShell powerShell = null;
    Console.WriteLine(" ");
    Console.WriteLine("Deploying application...");

    try
    {
        using (powerShell = PowerShell.Create())
        {
            powerShell.AddScript("$setup=Start-Process '" + executableFilePath + "' -ArgumentList '/s /v /qn /min' -Wait -PassThru");

            Collection<PSObject> PSOutput = powerShell.Invoke();
            foreach (PSObject outputItem in PSOutput)
            {

                if (outputItem != null)
                {

                    Console.WriteLine(outputItem.BaseObject.GetType().FullName);
                    Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
                }
            }

            if (powerShell.Streams.Error.Count > 0)
            {
                string temp = powerShell.Streams.Error.First().ToString();
                Console.WriteLine("Error: {0}", temp);

            }
            else
                Console.WriteLine("Installation has completed successfully.");

        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error occured: {0}", ex.InnerException);
    }
    finally
    {
        if (powerShell != null)
            powerShell.Dispose();
    }

}

OPTION-1和OPTION-2都打开了设置向导UI。没有安装过程启动,并且计算机上未安装任何应用程序。执行此代码时没有错误。

质询

  1. 我错过了OPTION-1/2中的任何内容吗?
  2. 上面的代码有什么问题?
  3. 我是否需要制作任何专门的InstallShield向导才能从此控制台应用程序中跳过UI?
  4. 为什么即使使用第三方应用程序(例如记事本++)也无法正常工作?
  5. 如何避免管理权限UI?

输出我想要

控制台应用程序必须静默安装.exe文件。必须跳过安装向导UI。如果出现控制台窗口,则可以。我不希望任何UI出现在控制台窗口之外。

c# wpf installation console-application silent-installer
1个回答
0
投票

最后,我找到了解决方案并解决了我的要求。

需要使用以下选项将app.manifest文件添加到WPF应用程序。应用必须以管理员权限打开。

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

更改静默安装的脚本。

    private void deployApplications(string executableFilePath)
    {
        PowerShell powerShell = null;
        Console.WriteLine(" ");
        Console.WriteLine("Deploying application...");
        try
        {
            using (powerShell = PowerShell.Create())
            {
                powerShell.AddScript(executableFilePath + " /S /v/qn");

                Collection<PSObject> PSOutput = powerShell.Invoke();
                foreach (PSObject outputItem in PSOutput)
                {

                    if (outputItem != null)
                    {
                        Console.WriteLine(outputItem.BaseObject.GetType().FullName);
                        Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
                    }
                }

                if (powerShell.Streams.Error.Count > 0)
                {
                    string temp = powerShell.Streams.Error.First().ToString();
                    Console.WriteLine("Error: {0}", temp);

                }
                else
                    Console.WriteLine("Installation has completed successfully.");

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error occured: {0}", ex.InnerException);
            //throw;
        }
        finally
        {
            if (powerShell != null)
                powerShell.Dispose();
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.