【问题标题】:C# Arguments for a specific process, open browser with url特定进程的 C# 参数,使用 url 打开浏览器
【发布时间】:2015-09-07 21:30:13
【问题描述】:

我正在编写一个应用程序,它应该在单击按钮时打开某个进程。但是,用户可以添加新按钮。我将以下代码用于在按钮单击时启动进程的操作:

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

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

提前谢谢你!

【问题讨论】:

  • 不确定我是否完全理解这一点。 ProgramPath 在哪里声明和实例化?

标签: c# .net string http arguments


【解决方案1】:

要启动一个进程以使用特定的url 打开浏览器,您可以试试这个:

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

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

但有时如果您的浏览器路径有问题,它就无法正常工作。下面的函数给你机器中浏览器的路径。

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

您可以使用浏览器的路径和网站的url,例如:

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

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

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

查看更多:

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

【讨论】:

  • 因此,使用它,我可以使用最常见 Web 浏览器的进程 ID,设置 if 或 case 语句,并在浏览器打开后使用用户输入的 URL 作为参数?
  • 谢谢!这对我帮助很大!
猜你喜欢
  • 2020-02-17
  • 2010-10-19
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 2014-12-06
  • 2014-02-10
  • 2021-11-09
  • 2012-07-12
相关资源
最近更新 更多