本文介绍了使用C#静默安装应用程序时跳过向导UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#控制台应用程序.它只有一个要求.它应该以静默方式安装.exe安装文件.这些安装文件是从InstallShield或其他产品生成的.我没有生成任何安装文件.我也尝试使用此代码安装Notepad ++,但操作不正常.

I am working on C# console application. It has only single requirement. It should silently install .exe setup files. These setup file are generated from InstallShield or other product. I do not generate any setup file. I tried to install Notepad++ using this code as well but it is not wokring.

为更具体起见,并避免对此问题重复标记,我已经提到了许多有关此问题的链接.其中一些是

To be more specific and to avoid duplicate mark to this question, I already referred many links regarding same. Some of them are

  1. http://www.c-sharpcorner.com/article/silent-installation-of-applications-using-c-sharp/
  2. C#代码在后台以静默方式运行我的installer.exe文件
  3. 如何在C#中运行无提示安装程序

我尝试的代码:

OPTION-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设置文件的路径.

executableFilePath is path to .exe setup file.

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.没有安装过程开始,计算机上也没有安装任何应用程序.执行此代码时没有错误.

Both OPTION-1 and OPTION-2 open setup wizard UI. There is no installtion process starts and no application gets installed on computer. There is no error while executing this code.

问题

  1. 我在OPTION-1/2中缺少任何内容吗?
  2. 上面的代码有什么问题?
  3. 我是否需要进行特殊的InstallShield向导才能从此控制台应用程序中跳过UI?
  4. 为什么即使在第三方应用程序(例如notepad ++)中也无法使用?
  5. 如何也避免管理权限用户界面?

我要输出

控制台应用程序必须以静默方式安装.exe文件.必须跳过安装向导的用户界面.如果出现控制台窗口,则很好.我不希望在控制台窗口之外出现任何用户界面.

Console application must install .exe files silently. Setup wizard UI must be skipped. It is fine if console window appears. I do not want any UI to be appear other than console window.

推荐答案

最后,我找到了解决方案并根据需要解决.

Finally , I found solution and work around for my requirement.

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

Need to add app.manifest file to the WPF application with the following option. App must open with the Administrator rights.

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

更改脚本以进行静默安装.

Change in script for silent installtion.

    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();
        }

    }

这篇关于使用C#静默安装应用程序时跳过向导UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:32
查看更多