使用C#代码通过命令提示符打开浏览器

时间:2016-07-01 21:08:49

标签: c# command-prompt

我正在尝试运行以下代码,该代码会打开命令提示符,然后传递打开chrome并导航到www.google.com的参数

浏览器需要从命令提示符打开。

我知道你传递参数时必须使用/ c。

我尝试了以下内容:

 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";

为什么浏览器没有打开而没有传递参数的任何想法?

代码

       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;
            }
       }

如果我手动执行,这就是它的样子。它会打开Chrome并传递参数。

enter image description here

1 个答案:

答案 0 :(得分:0)

使用此:

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

也就是说,在命令提示符下,这将等于:

C:\>cmd /c ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http://www.google.de"