2011-01-24 61 views
0

我尝试了所有我能想到的和每个可以想象的代码示例,但打开浏览器时无法使用Process.Start获取任何类型的输出。我试着只看错误输出和使用实际URL引发404错误和标准输出 - 没有任何作用。这是最简单的例子 - 尽管它失败,那么即使浏览器启动每次...ProcessStartInfo浏览器输出

 //Default Browser 
     RegistryKey key = null; 
     string defaultPath = ""; 

     try 
     { 
      key = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false); 
      defaultPath = key.GetValue("").ToString().ToLower().Replace("\"", ""); 
      if (!defaultPath.EndsWith(".exe")) 
       defaultPath = defaultPath.Substring(0, defaultPath.LastIndexOf(".exe") + 4); 
     } 
     catch { } 
     finally 
     { 
      if (key != null) 
       key.Close(); 
     } 

无工作代码:

 ProcessStartInfo browserInfo = new ProcessStartInfo(); 
     browserInfo.CreateNoWindow = true; 
     browserInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     browserInfo.FileName = defaultPath; 
     browserInfo.Arguments = "http://www.google.com"; 
     browserInfo.UseShellExecute = false; 
     browserInfo.RedirectStandardError = true; 
     browserInfo.RedirectStandardOutput = true; 
     string error = ""; 
     string output = ""; 
     String strProcessResults; 

     try 
     { 
      // Start the child process. 

      Process p = new Process(); 

      // Redirect the output stream of the child process. 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardError = true; 
      p.StartInfo.FileName = defaultPath; 
      p.StartInfo.Arguments = "http://www.google.com/NoneYa.html"; 
      p.Start(); 

      // Read the output stream first and then wait. 
      strProcessResults = p.StandardError.ReadToEnd(); 
      p.WaitForExit(); 
     } 
     catch (System.ComponentModel.Win32Exception BrowserX) 
     { 
      //We ignore the error if a browser does not exist! 
      if (BrowserX.ErrorCode != -2147467259) 
       throw BrowserX; 
     } 

OR

 try 
     { 
      // Start the child process. 

      Process p = new Process(); 

      // Redirect the output stream of the child process. 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardError = true; 
      p.StartInfo.FileName = defaultPath; 
      p.StartInfo.Arguments = "http://www.google.com"; 
      p.Start(); 

      // Read the output stream first and then wait. 
      strProcessResults = p.StandardOutput.ReadToEnd(); 
      p.WaitForExit(); 
     } 
     catch (System.ComponentModel.Win32Exception BrowserX) 
     { 
      //We ignore the error if a browser does not exist! 
      if (BrowserX.ErrorCode != -2147467259) 
       throw BrowserX; 
     } 

回答

0

我已经从未实际检查过,但如果您的浏览器将任何内容打印到标准输出,我会感到惊讶。这是一个Windowed应用程序。

+0

事实证明是这样 - 我最终用C++编写了一个内核监视器,拦截这些调用并将它们单独打印出来。 – 2012-04-23 11:31:06