2016-06-10 85 views
1

我現在有以下代碼經由一個按鈕發起數據網格內的WPF應用程序:在私人模式C#打開默認瀏覽器

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" 
+0

可能重複[如何在隱私瀏覽模式下打開鏈接](http://stackoverflow.com/questions/16148136/how-can-we-open-a-link-in-private-browsing-模式) –

+0

[我的瀏覽器](http://lynx.browser.org)沒有隱私瀏覽模式。 –

+1

@ Dr.Stitch不是重複的,因爲另一個問題是關於從網站開啓私人模式,而這個問題是從本地桌面應用程序啓動一個新的瀏覽器。 – NineBerry

回答

0

這是我的使用來自其他人民的帖子的最終工作代碼:

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

我假設你正試圖從一個控制檯或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(); 
} 
1

嘗試下面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; 
    } 
} 
0

希望這有助於爲Firefox和Chrome

測試它
public static void GetPrivateBrowserPath() 
    { 

     //string standardBrowserPath = [email protected]"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"; 
     string standardBrowserPath = [email protected]"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); 
    }