【问题标题】:C# Open Default browser in Private modeC#以私有模式打开默认浏览器
【发布时间】:2016-10-11 01:31:40
【问题描述】:

我目前有以下代码通过 WPF 应用程序的数据网格内的按钮启动:

private static string GetStandardBrowserPath()
{
    string browserPath = string.Empty;
    RegistryKey browserKey = null;

    try
    {
        //Read default browser path from Win XP registry key
        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(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
        }

        //If browser path was found, clean it
        if (browserKey != null)
        {
            //Remove quotation marks

            browserPath = (browserKey.GetValue(null) as string).ToLower().Replace("\"", "");

            //Cut off optional parameters
            if (!browserPath.EndsWith("exe"))
            {
                browserPath = browserPath.Substring(0, browserPath.LastIndexOf(".exe") + 4);
            }

            //Close registry key
            browserKey.Close();
        }
    }
    catch
    {
        //Return empty string, if no path was found
        return string.Empty;
    }
    //Return default browsers path
    return browserPath;
}

这样返回浏览器路径如下:

"c:\\program files\\mozilla firefox\\firefox.exe -osint -url %1"

我想要做的是在 .exe 的末尾添加另一个命令行以强制浏览器以私有模式打开,最终用户知道这会发生,但我不确定如何执行此操作。我想使用的浏览器是:

  • 谷歌浏览器
  • 火狐
  • 歌剧
  • 互联网探索

例如

"c:\\program files\\mozilla firefox\\firefox.exe -private -osint -url %1"

"c:\\program files (x86)\\google\\chrome\\application\\chrome.exe -incognito -- %1"

【问题讨论】:

标签: c# wpf winforms


【解决方案1】:

我假设您正在尝试从控制台、Windows 窗体或 WPF 应用程序运行它?您可以使用 .Net Process 来执行此操作。类似的东西:

var targetSite= "<website you want to open>";

using (var process = new Process())
{
    // Get Default Browser from Registry here
    // see http://stackoverflow.com/questions/13621467/how-to-find-default-web-browser-using-c

    switch (browserType)
    { 
          case "Chrome":
             process.StartInfo.FileName = @"<path to your chrome browser exe>";
             process.StartInfo.Arguments = url + " --incognito";
             break;
          case "Mozilla":
             ...... 
          default: 
            // Open in IE?


    process.Start();
}

【讨论】:

    【解决方案2】:

    code下方尝试使用默认浏览器打开特定的url,即使使用private mode

    string BrowserName = string.Empty;
    using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"))
    {
        if (userChoiceKey == null)
        {
            BrowserName = "UNKNOWN";
        }
    
        object progIdValue = userChoiceKey.GetValue("Progid");
        if (progIdValue == null)
        {
            BrowserName = "UNKNOWN";
        }
    
        switch (progIdValue.ToString())
        {
            case "IE.HTTP":
                BrowserName = "INTERNETEXPLORER";
                break;
            case "FirefoxURL":
                BrowserName = "FIREFOX";
                break;
            case "ChromeHTML":
                BrowserName = "CHROME";
                break;
            default:
                BrowserName = "UNKNOWN";
                break;
        }
    
        string url = "http://www.google.com";
    
        switch (BrowserName)
        {
            case "INTERNETEXPLORER":
                System.Diagnostics.Process.Start("iexplore.exe", "-private " + url);
                break;
            case "FIREFOX":
                System.Diagnostics.Process.Start("firefox.exe", "-private-window " + url);
                break;
            case "CHROME":
                System.Diagnostics.Process.Start("chrome.exe", "-incognito " + url);
                break;
            case "UNKNOWN":
                System.Diagnostics.Process.Start("iexplore.exe", "-private " + url);
                break;
        }
    }
    

    【讨论】:

      【解决方案3】:

      希望对你有帮助,用 Firefox 和 Chrome 测试过

       public static void GetPrivateBrowserPath()
          {
      
              //string standardBrowserPath = $@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
              string standardBrowserPath = $@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
              string privateModeParam = string.Empty;
      
              if (standardBrowserPath.ToUpper().Contains("FIREFOX"))
                  privateModeParam =  "-private-window";
      
              if (standardBrowserPath.ToUpper().Contains("CHROME"))
                  privateModeParam = "-incognito";
              Process.Start(standardBrowserPath,  privateModeParam );
          }
      

      【讨论】:

        【解决方案4】:

        这是我使用其他人帖子中的元素的最终工作代码:

        private void LaunchURLButton_Click(object sender, RoutedEventArgs e)
        {
            object url = ((Button) sender).CommandParameter;
            string privateModeParam = string.Empty;
            string UrlFromDb = (string) url;
            string browserName = GetDefaultBrowserPath();
            if (string.IsNullOrEmpty(browserName))
            {
                MessageBox.Show("no default browser found!");
            }
            else
        
            {
                if (browserName.Contains("firefox"))
                {
                    privateModeParam = " -private-window";
                }
                else if ((browserName.Contains("iexplore")) || (browserName.Contains("Opera")))
                {
                    privateModeParam = " -private";
                }
                else if (browserName.Contains("chrome"))
                {
                    privateModeParam = " -incognito";
                }
                Process.Start(browserName, $"{privateModeParam} {UrlFromDb}");
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-05-09
          • 2021-03-26
          • 1970-01-01
          • 2011-06-02
          • 2011-01-19
          • 2011-07-01
          • 2011-11-19
          • 1970-01-01
          相关资源
          最近更新 更多