簡體   English   中英

使用C#靜默安裝應用程序時跳過向導UI

[英]Skip wizard UI while silent installation of application using C#

我正在使用C#控制台應用程序。 它只有一個要求。 它應該靜默安裝.exe安裝文件。 這些安裝文件是從InstallShield或其他產品生成的。 我沒有生成任何安裝文件。 我也嘗試使用此代碼安裝Notepad ++,但操作不正常。

為了更具體和避免重復標記這個問題,我已經提到了許多有關此問題的鏈接。 他們之中有一些是

  1. http://www.c-sharpcorner.com/article/silent-installation-of-applications-using-c-sharp/
  2. C#代碼在后台以靜默模式運行我的installer.exe文件,
  3. 如何在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);

選項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. 為什么即使在第三方應用程序(例如notepad ++)中也無法使用?
  5. 如何也避免管理權限用戶界面?

我想要的輸出

控制台應用程序必須靜默安裝.exe文件。 必須跳過安裝向導的用戶界面。 如果出現控制台窗口,則很好。 我不希望控制台界面以外的任何UI出現。

最終,我找到了解決方案並根據需要解決。

需要使用以下選項將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();
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM