首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProcessStartInfo浏览器输出

ProcessStartInfo浏览器输出
EN

Stack Overflow用户
提问于 2011-01-25 06:26:15
回答 1查看 388关注 0票数 0

我已经尝试了我能想到的所有方法和所有能想到的代码样本,但在打开浏览器时,我无法使用Process.Start获得任何类型的输出。我试着只查看错误输出,并使用实际的URL引出404个错误和标准输出-什么都不起作用。下面是一个最简单的例子--尽管浏览器每次都会启动,但它也会失败。

代码语言:javascript
复制
        //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();
        }

无工作代码:

代码语言:javascript
复制
        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;
        }

代码语言:javascript
复制
        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;
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-25 06:30:57

我实际上从未检查过,但如果您的浏览器将任何内容打印到stdout,我会感到惊讶。这是一个窗口应用程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4787937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档