2009-05-01 7 views
7

Da quale RegKey è possibile ottenere il percorso dell'applicazione browser predefinito?RegKey di Windows - Percorso applicazione browser predefinito

Il modo migliore per arrivare da C# /. NET?

+3

Non cercare il Registro di sistema per tentare di avviare il browser Web predefinito. Cosa stai cercando di fare? – Michael

+0

Non volevo avviare il browser predefinito. Avevo un programma in grado di fare alcune scelte diverse sulla base di quale fosse la preferenza di un browser. – BuddyJoe

risposta

16

Ecco la chiave che si desidera:

HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \ http open \ command shell \ \

Ed ecco una rapida registry tutorial for C#, se ne avete bisogno.

Edit:

Per le impostazioni per utente, utilizzare questa chiave:

HKEY_CLASSES_ROOT \ http comando \ open shell \ \

(HKCR ha sia la macchina e l'utente impostazioni, l'utente ha priorità).

Si noti che questo potrebbe non funzionare su Vista. Per maggiori informazioni, see here.

+0

Ma suppongo che intendi in HKEY_CURRENT_USER giusto? – BuddyJoe

+0

Non c'è una chiave corrispondente in HKCU. Vedi la mia modifica per maggiori informazioni. –

+0

fantastico. Grazie. – BuddyJoe

1

per le finestre percorso del browser 7 di salvataggio predefinito nella seguente chiave di registro

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http 

utilizzando C# si può ottenere come segue -

RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false); 

string browser = regkey.GetValue("Progid").ToString(); 
1

In base alle vostre risposte ho scritto questo codice di esempio che dovrebbe fai quello che vuoi (non testato)

public static string GetDefaultBrowserPath() 
    { 
     string defaultBrowserPath = null; 
     RegistryKey regkey; 

     // Check if we are on Vista or Higher 
     OperatingSystem OS = Environment.OSVersion; 
     if ((OS.Platform == PlatformID.Win32NT) && (OS.Version.Major >= 6)) 
     { 
      regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false); 
      if (regkey != null) 
      { 
       defaultBrowserPath = regkey.GetValue("Progid").ToString(); 
      } 
      else 
      { 
       regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\IE.HTTP\\shell\\open\\command", false); 
       defaultBrowserPath = regkey.GetValue("").ToString(); 
      } 
     } 
     else 
     { 
      regkey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", false); 
      defaultBrowserPath = regkey.GetValue("").ToString(); 
     } 

     return defaultBrowserPath; 
    } 
+2

Su Win7, comunque, "Progid" non sembra contenere link. Contiene un ID programma da cercare nel registro sotto "HKCR/FetchedProgramId" (con FetchedProgramId l'ID del programma recuperato prima). Sotto quella chiave c'è, ancora, un "comando \ shell \ apri \", in cui trovi il percorso attuale. – Nyerguds

+0

Questo non sembra dare il percorso su Windows 10. Ha restituito solo un valore di IE.HTTP – Fractal

0

Ho appena creato una funzione per questo:

public void launchBrowser(string url) 
    { 
     string browserName = "iexplore.exe"; 
     using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice")) 
     { 
      if (userChoiceKey != null) 
      { 
       object progIdValue = userChoiceKey.GetValue("Progid"); 
       if (progIdValue != null) 
       { 
        if(progIdValue.ToString().ToLower().Contains("chrome")) 
         browserName = "chrome.exe"; 
        else if(progIdValue.ToString().ToLower().Contains("firefox")) 
         browserName = "firefox.exe"; 
        else if (progIdValue.ToString().ToLower().Contains("safari")) 
         browserName = "safari.exe"; 
        else if (progIdValue.ToString().ToLower().Contains("opera")) 
         browserName = "opera.exe"; 
       } 
      } 
     } 

     Process.Start(new ProcessStartInfo(browserName, url)); 
    }