2009-04-20 58 views
16

我如何得到一般的文件類型描述基於擴展名像Explorer那樣嗎?所以不是MIME,而是最終用戶看到的信息。如何根據擴展名獲取文件類型信息? (而不是MIME)在c#

.doc = Microsoft Office Word 97 - 2003文檔 .zip = ZIP文件 .avi =視頻文件。

如何獲得似乎可用的「輔助」信息,我猜這不是基於擴展名。就像在「視頻文件」它可以給你的'長度'的電影或doc文件有多少網頁它等..等。

回答

26

謝謝丹,好吧..這回答我的第一個問題。可惜不是第二個。注:並非所有的打印.. 貸PInvoke.net

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Diagnostics; 


namespace WindowsFormsApplication1 
{ 
    static class Program 
    { 
     [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); 

     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Debug.WriteLine(FileExtentionInfo(AssocStr.Command, ".doc"), "Command"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.DDEApplication, ".doc"), "DDEApplication"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.DDEIfExec, ".doc"), "DDEIfExec"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.DDETopic, ".doc"), "DDETopic"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.Executable, ".doc"), "Executable"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.FriendlyAppName, ".doc"), "FriendlyAppName"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.FriendlyDocName, ".doc"), "FriendlyDocName"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.NoOpen, ".doc"), "NoOpen"); 
      Debug.WriteLine(FileExtentionInfo(AssocStr.ShellNewValue, ".doc"), "ShellNewValue"); 

      // DDEApplication: WinWord 
      //DDEIfExec: Ñﻴ߾ 
      // DDETopic: System 
      // Executable: C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE 
      // FriendlyAppName: Microsoft Office Word 
      // FriendlyDocName: Microsoft Office Word 97 - 2003 Document 


     } 

     public static string FileExtentionInfo(AssocStr assocStr, string doctype) 
     { 
      uint pcchOut = 0; 
      AssocQueryString(AssocF.Verify, assocStr, doctype, null, null, ref pcchOut); 

      StringBuilder pszOut = new StringBuilder((int)pcchOut); 
      AssocQueryString(AssocF.Verify, assocStr, doctype, null, 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 
     } 

    } 
} 
+0

我覺得你的兩個問題是非常不相關的。 – 2009-04-23 13:17:53

4

直接從註冊表中讀取像這樣的東西通常是一個壞主意(所有gory details參見Raymond Chen's blog)。在這個特定的情況下,你想要的API是AssocQueryStringshlwapi.h

這裏的C++代碼:

TCHAR buf[1024]; 
DWORD sz = sizeof(buf)/sizeof(TCHAR); 
AssocQueryString(ASSOCF_INIT_DEFAULTTOSTAR, ASSOCSTR_FRIENDLYDOCNAME, L".sql", NULL, buf, &sz); 

你可以使用這個從C#或者通過C++/CLI露出一個不錯的.NET友好的API;或直接通過P/Invoke致電。

3

一些額外的,如果對XP中的未知文件類型.. 未必真有什麼,但FriendlyDocName使用時給出正確的結果,但只是作爲一個例子:

public static string FileExtentionInfo(AssocStr assocStr, string doctype) 
{ 
    if ((doctype.Length <= 1) || !doctype.StartsWith(".")) return ""; 

    uint pcchOut = 0; 
    AssocQueryString(AssocF.Verify, assocStr, doctype, null, null, ref pcchOut); 

    if (pcchOut == 0) return (doctype.Trim('.').ToUpper() + " File"); 

    StringBuilder pszOut = new StringBuilder((int)pcchOut); 
    AssocQueryString(AssocF.Verify, assocStr, doctype, null, pszOut, ref pcchOut); 
    return pszOut.ToString(); 
} 
+0

做得很好的Pjanssen,我剛剛發現原來的功能在XP下無法使用!良好的工作,爲我省下了自己必須做的時間! – JustAPleb 2010-02-14 11:56:24

3

我的代碼,其中包括檢查一些常見的錯誤,以防止...希望這有助於:-)

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"); 
    /// </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 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 
     } 



    } 
} 
相關問題