有没有人知道如何获得默认浏览器,而无需查找使用.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