C# 在私有模式下打开默认浏览器 [英] C# Open Default browser in Private mode

查看:39
本文介绍了C# 在私有模式下打开默认浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have the following code that is launched via a button within a data grid on a WPF application:

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 的末尾添加另一个命令行以强制浏览器以私有模式打开,最终用户知道这会发生,但我不确定如何执行此操作.我想使用的浏览器是:

What I want to do is add another command line onto the end of the .exe to force the browser to open in private mode and the end user knows this will happen, but I am unsure how to do this. The browsers I want to do this with are:

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

例如

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

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

推荐答案

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

This was my final working code using elements from other peoples posts:

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

这篇关于C# 在私有模式下打开默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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