我已经尝试了我能想到的所有东西以及可以想象的每个代码示例,但是在打开浏览器时我无法使用 Process.Start 获得任何类型的输出。我试过只查看错误输出并使用实际 URL 引发 404 错误和标准输出 - 没有任何效果。这是最简单的示例-尽管即使浏览器每次都启动它也会失败...
//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;
}
或者
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;
}