Windows RegKey - Chemin d’application du navigateur par défaut
Question
À partir de quelle clé RegKey pouvez-vous obtenir le chemin de l'application de navigateur par défaut?
Le meilleur moyen d’y accéder à partir de C # /. NET?
La solution
Voici la clé que vous voulez:
HKEY_LOCAL_MACHINE \ LOGICIEL \ Classes \ http \ shell \ open \ command
Et voici un registre pour C # , si vous en avez besoin.
Modifier :
Pour les paramètres par utilisateur, utilisez cette clé:
HKEY_CLASSES_ROOT \ http \ shell \ open \ command
(HKCR a les paramètres machine et utilisateur, l'utilisateur a la priorité).
Notez que cela pourrait ne pas fonctionner sous Vista ??strong>. Pour plus d'informations, voir ici .
Autres conseils
pour le chemin de navigateur par défaut de Windows 7, enregistrez-le dans la clé de registre suivante
HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http
en utilisant c #, vous pouvez l'obtenir comme suit -
RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false);
string browser = regkey.GetValue("Progid").ToString();
Sur la base de vos réponses, j'ai écrit cet exemple de code qui devrait faire ce que vous voulez (non testé)
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;
}
Je viens de créer une fonction pour cela:
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));
}