Voici mon code complet comment ouvrir.
il y a 2 options :
-
ouvrir en utilisant le navigateur par défaut (le comportement est le même que pour l'ouverture dans la fenêtre du navigateur)
-
ouvrir par le biais des options de commande par défaut (le comportement est le même que lorsque vous utilisez la commande "RUN.EXE")
-
ouvrir à travers 'explorer' (le comportement est comme si vous aviez écrit l'url dans l'url de votre fenêtre de dossier)
[4. utilisez l'emplacement du processus d'iexplore pour ouvrir l'url requise.
CODE :
internal static bool TryOpenUrl(string p_url)
{
// try use default browser [registry: HKEY_CURRENT_USER\Software\Classes\http\shell\open\command]
try
{
string keyValue = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\http\shell\open\command", "", null) as string;
if (string.IsNullOrEmpty(keyValue) == false)
{
string browserPath = keyValue.Replace("%1", p_url);
System.Diagnostics.Process.Start(browserPath);
return true;
}
}
catch { }
// try open browser as default command
try
{
System.Diagnostics.Process.Start(p_url); //browserPath, argUrl);
return true;
}
catch { }
// try open through 'explorer.exe'
try
{
string browserPath = GetWindowsPath("explorer.exe");
string argUrl = "\"" + p_url + "\"";
System.Diagnostics.Process.Start(browserPath, argUrl);
return true;
}
catch { }
// return false, all failed
return false;
}
et la fonction Helper :
internal static string GetWindowsPath(string p_fileName)
{
string path = null;
string sysdir;
for (int i = 0; i < 3; i++)
{
try
{
if (i == 0)
{
path = Environment.GetEnvironmentVariable("SystemRoot");
}
else if (i == 1)
{
path = Environment.GetEnvironmentVariable("windir");
}
else if (i == 2)
{
sysdir = Environment.GetFolderPath(Environment.SpecialFolder.System);
path = System.IO.Directory.GetParent(sysdir).FullName;
}
if (path != null)
{
path = System.IO.Path.Combine(path, p_fileName);
if (System.IO.File.Exists(path) == true)
{
return path;
}
}
}
catch { }
}
// not found
return null;
}
J'espère avoir aidé.