0

I am trying to run the following code which opens command prompt and then passes the parameters which opens chrome and navigates to www.google.com

The browser needs to open from Command Prompt.

I know you have to use /c when you pass arguments.

I have tried the following:

 string arguments = "/c " + "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"" + " www.google.com";
 string arguments = "/c " + "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " + "www.google.com";

Any ideas why the browser is not opening and not passing the parameters?

Code

       public void ExecuteCmd()
       {

            int exitCode;
            string arguments ="/c " + @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe " + "www.google.com";
            // Prepare the process to run
            ProcessStartInfo start = new ProcessStartInfo();
            // Enter in the command line arguments, everything you would enter after the executable name itself
            start.Arguments = arguments;

            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            // Enter the executable to run, including the complete path
            start.FileName = @"C:\Windows\system32\cmd.exe";
            // Do you want to show a console window?
            start.WindowStyle = ProcessWindowStyle.Hidden;
            start.CreateNoWindow = true;

            // Run the external process & wait for it to finish
            using (Process proc = Process.Start(start))
            {
                string output = proc.StandardOutput.ReadToEnd(); 
                proc.WaitForExit();
                // Retrieve the app's exit code
                exitCode = proc.ExitCode;
            }
       }

Here is what it looks like if I do it manually. It opens Chrome and passes the parameter.

enter image description here

Apollo
  • 1,990
  • 12
  • 44
  • 65
  • You don't have to use '/c' when you pass arguments. That switch only means that the command prompt will be terminated after the command is executed. For more information, open a command prompt and type: `cmd /?` – Rufus L Jul 02 '16 at 00:14
  • When I copy and paste your main code and replace 'arguments' with the very first example of 'arguments' that you initially specified, it works just fine for me (the problem with your main code 'arguments' and the second 'arguments' example is that you don't have extra quotes around the path to chrome.exe, which is required because the path contains spaces). – Rufus L Jul 02 '16 at 00:32
  • @RufusL At least on Windows 7, it is necessary to either use `/c` or `/k`, or otherwise nothing will happen, and that's what `cmd /?` tells. I don't know, maybe this changed with Win 8 or 10. – Martin Jul 02 '16 at 23:40
  • Possible duplicate of [How to open in default browser in C#](https://stackoverflow.com/questions/4580263/how-to-open-in-default-browser-in-c-sharp) – Ray Dec 08 '18 at 12:55

1 Answers1

2

Use this:

string arguments = @"/c """"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" http://www.google.de"""

That is, on the command prompt this would be equal to:

C:\>cmd /c ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http://www.google.de"
Martin
  • 1,986
  • 15
  • 32