ProcessStartInfo 浏览器输出

发布于 2024-10-14 05:33:32 字数 2900 浏览 3 评论 0原文

我已经尝试了我能想到的所有内容以及可以想象到的每个代码示例,但在打开浏览器时我无法使用 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;
        }

I have tried everything I can think of and every code sample imaginable but I cannot get any type of output using Process.Start when opening a browser. I have tried just looking at error output and eliciting 404 errors and Standard Output using actual URLs - nothing works. Here is the simplest example - although it fails as well even though the browser launches every time...

        //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();
        }

None Working Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-10-21 05:33:32

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

I've never actually checked, but I'd be surprised if your browser printed anything to stdout. It's a Windowed application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文