2011-01-24 62 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