2010-06-11 29 views

回答

0

为什么你想避免注册表查找?

您可以使用AssocQueryString Function,但它在内部使用注册表来获取默认浏览器。这里是C#中的一个例子:Getting the Default Browser Path.

+0

好问题,到目前为止,该项目能够避免任何注册表查找,这将是很好的保持这种策略。在此之外,如果有一个.NET的电话,这种方式应该从我的角度来看应该是首选。感谢您的输入,我会测试它 – Patrick 2010-06-11 07:18:46

+0

如果有专门的API调用可以避免需要了解注册表,那么使用它是有意义的?Microsoft可以将内部实现更改为需要。 – Thorarin 2010-06-11 08:06:12

+0

是的,这也是我的观点。 – Patrick 2010-06-11 08:39:20

1

那么,你可以运行System.Diagnostics.Process.Start(url);这将启动默认的浏览器,然后你可以检查进程运行的是什么可执行文件,但我不知道它是否有很大的区别,因为Windows会使用注册表来决定哪个是默认的。

+0

我也试过这个,并且不得不等待20秒直到浏览器出现。所以这种方法是不可接受的。 – Patrick 2010-06-11 07:36:32

+0

只要IE是系统上唯一的浏览器,这可能会正常工作。 – Patrick 2010-06-11 07:40:10

+0

@Patrick:看看你对Giorgi的评论,似乎离开注册表的理由是为了保持代码整洁,而不是任何技术原因,所以我建议它实际上最好是从直接注册表,你做的任何事情都会从那里读取它(因为这是它存储的唯一的地方,据我所知),你的代码和注册表之间只会有更多的层次。 – 2010-06-11 07:50:08

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 
     } 



    } 
} 
相关问题