有沒有人知道如何獲得默認瀏覽器,而無需查找使用.NET框架的註冊表?如何在.NET中不使用註冊表查找來獲取默認瀏覽器?
0
A
回答
0
爲什麼你想避免註冊表查找?
您可以使用AssocQueryString Function,但它在內部使用註冊表來獲取默認瀏覽器。這裏是C#中的一個例子:Getting the Default Browser Path.
1
那麼,你可以運行System.Diagnostics.Process.Start(url);
這將啓動默認的瀏覽器,然後你可以檢查進程運行的是什麼可執行文件,但我不知道它是否有很大的區別,因爲Windows會使用註冊表來決定哪個是默認的。
0
文件關聯是操作系統查找關聯程序進行擴展的方式。註冊表是Windows中文件關聯的中心條目。默認瀏覽器與名「.html」或.htm」相關聯的一個。
這是我的代碼,以查找文件關聯,其中包括一些檢查從一些常見錯誤,以防止...希望它有助於:-)
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace HQ.Util.Unmanaged
{
/// <summary>
/// Usage: string executablePath = FileAssociation.GetExecFileAssociatedToExtension(pathExtension, "open");
/// Usage: string command FileAssociation.GetExecCommandAssociatedToExtension(pathExtension, "open");
/// </summary>
public static class FileAssociation
{
/// <summary>
///
/// </summary>
/// <param name="ext"></param>
/// <param name="verb"></param>
/// <returns>Return null if not found</returns>
public static string GetExecCommandAssociatedToExtension(string ext, string verb = null)
{
if (ext[0] != '.')
{
ext = "." + ext;
}
string executablePath = FileExtentionInfo(AssocStr.Command, ext, verb);
// Ensure to not return the default OpenWith.exe associated executable in Windows 8 or higher
if (!string.IsNullOrEmpty(executablePath) && File.Exists(executablePath) &&
!executablePath.ToLower().EndsWith(".dll"))
{
if (executablePath.ToLower().EndsWith("openwith.exe"))
{
return null; // 'OpenWith.exe' is th windows 8 or higher default for unknown extensions. I don't want to have it as associted file
}
return executablePath;
}
return executablePath;
}
/// <summary>
///
/// </summary>
/// <param name="ext"></param>
/// <param name="verb"></param>
/// <returns>Return null if not found</returns>
public static string GetExecFileAssociatedToExtension(string ext, string verb = null)
{
if (ext[0] != '.')
{
ext = "." + ext;
}
string executablePath = FileExtentionInfo(AssocStr.Executable, ext, verb); // Will only work for 'open' verb
if (string.IsNullOrEmpty(executablePath))
{
executablePath = FileExtentionInfo(AssocStr.Command, ext, verb); // required to find command of any other verb than 'open'
// Extract only the path
if (!string.IsNullOrEmpty(executablePath) && executablePath.Length > 1)
{
if (executablePath[0] == '"')
{
executablePath = executablePath.Split('\"')[1];
}
else if (executablePath[0] == '\'')
{
executablePath = executablePath.Split('\'')[1];
}
}
}
// Ensure to not return the default OpenWith.exe associated executable in Windows 8 or higher
if (!string.IsNullOrEmpty(executablePath) && File.Exists(executablePath) &&
!executablePath.ToLower().EndsWith(".dll"))
{
if (executablePath.ToLower().EndsWith("openwith.exe"))
{
return null; // 'OpenWith.exe' is th windows 8 or higher default for unknown extensions. I don't want to have it as associted file
}
return executablePath;
}
return executablePath;
}
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);
private static string FileExtentionInfo(AssocStr assocStr, string doctype, string verb)
{
uint pcchOut = 0;
AssocQueryString(AssocF.Verify, assocStr, doctype, verb, null, ref pcchOut);
Debug.Assert(pcchOut != 0);
if (pcchOut == 0)
{
return "";
}
StringBuilder pszOut = new StringBuilder((int)pcchOut);
AssocQueryString(AssocF.Verify, assocStr, doctype, verb, pszOut, ref pcchOut);
return pszOut.ToString();
}
[Flags]
public enum AssocF
{
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200
}
public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic
}
}
}
相關問題
- 1. 如何從默認瀏覽器獲取默認主頁
- 2. 在註冊表中查找Chrome瀏覽器版本
- 3. Windows註冊表 - 默認瀏覽器的關鍵
- 4. 用於「檢查IE是否爲默認瀏覽器」的Windows註冊表項?
- 5. 使用JavaScript查找CSS屬性的瀏覽器默認值
- 6. 如何在VBScript中從註冊表中讀取(默認)值?
- 7. java是否使用註冊表來查找其默認jar文件?
- 8. 如何使用QSettings讀取Windows註冊表(默認)值?
- 9. 如何找到瀏覽器的默認樣式表?
- 10. 如何在運行.net項目時更改默認瀏覽器
- 11. 如何在自動測試中使用android默認瀏覽器
- 12. 如何使用JavaMe在默認瀏覽器中打開鏈接?
- 13. 如何獲取系統的默認瀏覽器?
- 14. 如何獲取默認的iOS瀏覽器名稱?
- 15. 使用註冊表或命令行獲取Microsoft Edge瀏覽器版本
- 16. Eclipse Web瀏覽器是否使用默認的OS瀏覽器?
- 17. 使用默認瀏覽器打開瀏覽器/ URL
- 18. 如何使用我的默認瀏覽器獲取prezto以打開github?
- 19. 設備/瀏覽器註冊
- 20. 使用默認的sql表來查找表中存在的列
- 21. 如何更改cordova瀏覽器平臺的默認瀏覽器?
- 22. 如何打開默認瀏覽器
- 23. 如何避免默認Android瀏覽器
- 24. web瀏覽器的I幀在默認瀏覽器中打開
- 25. 如何強制瀏覽器DOMDocument.ExecCommand()函數注入默認字體
- 26. System.Uri(「」)在默認瀏覽器中?
- 27. 瀏覽器默認樣式
- 28. 瀏覽器默認填充
- 29. 默認瀏覽器IE
- 30. 瀏覽器字體默認
好問題,到目前爲止,該項目能夠避免任何註冊表查找,這將是很好的保持這種策略。在此之外,如果有一個.NET的電話,這種方式應該從我的角度來看應該是首選。感謝您的輸入,我會測試它 – Patrick 2010-06-11 07:18:46
如果有專門的API調用可以避免需要了解註冊表,那麼使用它是有意義的?Microsoft可以將內部實現更改爲需要。 – Thorarin 2010-06-11 08:06:12
是的,這也是我的觀點。 – Patrick 2010-06-11 08:39:20