Question

J'ai essayé tout ce que je peux penser et tous les exemples de code imaginables, mais je ne peux pas obtenir tout type de sortie en utilisant Process.Start lors de l'ouverture d'un navigateur. J'ai essayé juste regarder la sortie d'erreur et provoquer des erreurs 404 et la sortie standard en utilisant les URL réelles - rien ne fonctionne. Voici l'exemple le plus simple - même si elle ne parvient aussi bien, même si les lancements de navigateur chaque fois que ...

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

Aucun Code de travail:

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

ou

        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;
        }
Était-ce utile?

La solution

Je ne l'ai jamais fait vérifié, mais je serais surpris si votre navigateur imprimé quoi que ce soit à la sortie standard. Il est une application fenêtré.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top