1

I am writing a C# Console App and I need to install dependencies using Chocolatey and then set up some config. The problem is I want to do all of these tasks in one Console. However, the below code example pops up a new Console window.

static void Main(string[] args) {
    // do some stuff
    System.Diagnostics.Process.Start("CMD.exe", "/C choco install nssm");
    // do other stuff
}

Is there any way I can do the setup part as a part of this console application?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Yar
  • 7,020
  • 11
  • 49
  • 69

1 Answers1

3

You can use another override of the Process.Start method that accepts a ProcessStartInfo argument. This class has a WindowStyle property that you can set to ProcessWindowStyle.Hidden. This will make whatever window created by the process be hidden.

ProcessStartInfo process_start_info = new ProcessStartInfo();

process_start_info.WindowStyle = ProcessWindowStyle.Hidden;

process_start_info.FileName = @"C:\ProgramData\chocolatey\bin\choco.exe";

process_start_info.Arguments = "install googlechrome";

var process = Process.Start(process_start_info);

process.WaitForExit();

Please note that I am executing choco.exe directly instead of executing the cmd.exe. But if you are able to use cmd.exe then this should not be an issue.

Please note also that I am storing the result of invoking Process.Start into a variable and then I am using the WaitForExit method to wait for the process to finish. After that you can simply start a new process to install a new package using similar code. You can encapsulate this functionality into some method and use it multiple times.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Well, that could be helpful but in this case it's not gonna work since you need to choose a couple of options during the installation through Chocolatey – Yar Nov 21 '15 at 06:19
  • @Hooman, you can redirect input and output between you and the process that you create. Here is a related question: http://stackoverflow.com/questions/21848271/redirecting-standard-input-of-console-application – Yacoub Massad Nov 21 '15 at 11:48
  • You can definitely do this kind of stuff. Another consideration you can do is to use the chocolatey.lib instead to install Chocolatey packages. – ferventcoder Nov 23 '15 at 04:38