Search code examples
vb.netcmdpathchocolatey

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


The first time I open and run the program it installs Choco but will not install the applications. If I close the application and run it again, it will then install the applications. My guess is the WinForm doesn't know it can use the choco command? Is there a way to have the application refresh its system?

When installing Chocolatey it does display this: You may need to shut down and restart powershell and/or consoles first prior to using choco.

So I might just be SOL, but I figured I couldnt be the first person to attempt to do this type of thing.

Below is my code for installing Chocolatey and then installing a list of applications using it.

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

Solution

  • Try installing Chocolatey and the packages in one call to RunCmd(). Either that or reference the absolute path of the Chocolatey executable when running the choco command. I believe right now it's C:\temp\MyChocolatey\bin\choco.exe.