1

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

This returns the browser path as follows:

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

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:

  • Google Chrome
  • Fire Fox
  • Opera
  • Internet Explore

e.g.

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

or

"c:\\program files (x86)\\google\\chrome\\application\\chrome.exe -incognito -- %1"
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
4d554d424c4553
  • 95
  • 2
  • 14
  • Possible duplicate of [How can we open a link in private browsing mode](http://stackoverflow.com/questions/16148136/how-can-we-open-a-link-in-private-browsing-mode) – Dr. Stitch Jun 10 '16 at 07:22
  • [My browser](http://lynx.browser.org) doesn't have a private browsing mode. – Damien_The_Unbeliever Jun 10 '16 at 07:26
  • 1
    @Dr.Stitch Not a duplicate, since the other question is about turning private mode on from a website while this question is starting a new browser from a local desktop application. – NineBerry Jun 10 '16 at 07:35

4 Answers4

3

try below code to open specific url using default browser even with 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;
    }
}
Darshan Faldu
  • 1,471
  • 2
  • 15
  • 32
2

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}");
    }
}
4d554d424c4553
  • 95
  • 2
  • 14
0

I assume you are trying to run this from a console or Windows Forms or WPF application? You can use the .Net Process to do this. Something like:

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();
}
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
0

Hope this helps, tested it with Firefox and 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 );
    }
Venkatesh Muniyandi
  • 5,132
  • 2
  • 37
  • 40