如何在我的应用程序中通过同一个调用安装 Choco 和几个应用程序?

How can I install Choco and a few applications from the same call in my application?

我第一次打开 运行 程序时它会安装 Choco 但不会安装应用程序。如果我关闭应用程序并再次 运行 它,它将安装应用程序。我的猜测是 WinForm 不知道它可以使用 choco 命令?有没有办法让应用程序刷新其系统?

安装 Chocolatey 时确实显示: 在使用 choco 之前,您可能需要先关闭并重新启动 powershell and/or 控制台。

所以我可能只是 SOL,但我想我不可能是第一个尝试做这种事情的人。

下面是我安装 Chocolatey 然后安装使用它的应用程序列表的代码。

Sub InstallChocoApps()
    RunCmd("@""%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"" -NoProfile -ExecutionPolicy Bypass -Command ""iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"" && SET ""PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin""", "", False, True)

    Dim Packages() As String = {"notepadplusplus.install", "7zip.install", "firefox", "googlechrome", "putty.install", "sumatrapdf.install", "vlc"}
        For Each p In Packages
            RunCmd("choco install " & p & " --force -y --no-progress", "", False, True)
        Next
End Sub

Sub RunCmd(command As String, arguments As String, permanent As Boolean, display As Boolean)
        Try
            Dim p As Process = New Process()
            Dim pi As ProcessStartInfo = New ProcessStartInfo()
            pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
            pi.FileName = "cmd.exe"
            pi.WindowStyle = ProcessWindowStyle.Hidden
            pi.CreateNoWindow = True
            pi.Verb = "runas"
            pi.UseShellExecute = False
            p.StartInfo = pi
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            p.Start()
            p.WaitForExit()
            p.Close()


        Catch ex As Exception
            LogData(ex.ToString())
        End Try
    End Sub

尝试一次调用 RunCmd() 安装 Chocolatey 和软件包。或者在 运行 choco 命令时引用 Chocolatey 可执行文件的绝对路径。我相信现在是 C:\temp\MyChocolatey\bin\choco.exe.