特定过程的C#参数,使用url打开浏览器 [英] C# Arguments for a specific process, open browser with url

查看:279
本文介绍了特定过程的C#参数,使用url打开浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应按一下按钮即可打开特定进程的应用程序.但是,用户可以添加新按钮.我在执行以下操作时使用了以下代码,该操作在单击按钮时启动了该过程:

I am writing an application that is supposed to open a certain process on the click of a button. However, the user has the ability to add new buttons. I'm using the following code for the action that occurs that starts the process on button click:

        private void StartProcess(string path)
    {
        ProcessStartInfo StartInformation = new ProcessStartInfo();

        StartInformation.FileName = path;

        Process process = Process.Start(StartInformation);

        process.EnableRaisingEvents = true;
    }
    private void ClickFunc(object sender, RoutedEventArgs e)
    {
        if (File.Exists(ProgramPath))
        {
            StartProcess(ProgramPath);
        }
        else
        {
            MessageBox.Show("Specified path does not exist, please try again.", "Bad File Path Error", MessageBoxButton.OK);
        }
    }

我要完成的工作是,当用户为网页创建一个按钮时,它会打开浏览器,然后打开该网页.有什么想法吗?

What I'm trying to accomplish is, when the user creates a button for a webpage, it opens the browser, then the webpage. Any ideas?

提前谢谢!

推荐答案

要启动使用特定url打开浏览器的过程,您可以尝试以下操作:

To start a process to open the browser with a specific url you can try this:

string url = "http://www.stackoverflow.com";

var process = System.Diagnostics.Process.Start(url);

但是有时候,如果您的浏览器路径出现问题,它将无法正常工作.下面的功能为您提供了机器中浏览器的路径.

But sometimes if you have problems with the path of your browser, it cannot work properly. The function bellow gives you the path of the browser in the machine.

public static string GetDefaultBrowserPath()
{
    string urlAssociation = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
    string browserPathKey = @"$BROWSER$\shell\open\command";

    RegistryKey userChoiceKey = null;
    string browserPath = "";

    try
    {
        //Read default browser path from userChoiceLKey
        userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + @"\UserChoice", false);

        //If user choice was not found, try machine default
        if (userChoiceKey == null)
        {
            //Read default browser path from Win XP registry key
            var browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

            //If browser path wasn’t found, try Win Vista (and newer) registry key
            if (browserKey == null)
            {
                browserKey =
                Registry.CurrentUser.OpenSubKey(
                urlAssociation, false);
            }
            var path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
            browserKey.Close();
            return path;
        }
        else
        {
            // user defined browser choice was found
            string progId = (userChoiceKey.GetValue("ProgId").ToString());
            userChoiceKey.Close();

            // now look up the path of the executable
            string concreteBrowserKey = browserPathKey.Replace("$BROWSER$", progId);
            var kp = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
            browserPath = CleanifyBrowserPath(kp.GetValue(null) as string);
            kp.Close();
            return browserPath;
        }
    }
    catch(Exception ex)
    {
        return "";  
    }
}

您可以使用浏览器的路径和网站的网址作为示例:

And you can use the path of the browser and the url of website, for sample:

string url = "http://www.stackoverflow.com";

var process = System.Diagnostics.Process.Start(GetDefaultBrowserPath(), url);

url字符串中,您可以传递网页链接.它将使用网址打开浏览器.

In the url string you can pass the webpage link. It will open the browser with the url.

查看更多:

http://www.seirer.net/blog/2014/6/10/solved-how-to-open-a-url-in-the-default-browser-in-csharp

这篇关于特定过程的C#参数,使用url打开浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆