2010-06-11 32 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 
     } 



    } 
} 
相關問題