2009-04-20 12 views
16

एक्सप्लोरर जैसे एक्सटेंशन के आधार पर सामान्य फ़ाइल प्रकार विवरण कैसे प्राप्त करें? तो एमआईएम नहीं, लेकिन अंत उपयोगकर्ता द्वारा देखी जाने वाली जानकारी, जैसे।विस्तार के आधार पर मुझे फ़ाइल प्रकार की जानकारी कैसे प्राप्त हो सकती है? (एमआईएमई नहीं) सी #

.doc = माइक्रोसॉफ़्ट ऑफिस वर्ड 97 - 2003 दस्तावेज़ । ज़िप = ज़िप फ़ाइल .avi = वीडियो फ़ाइल।

और मैं उपलब्ध 'माध्यमिक' जानकारी कैसे प्राप्त कर सकता हूं, जो मुझे लगता है कि यह एक्सटेंशन आधारित नहीं है। "वीडियो फाइलों" की तरह यह आपको फिल्म की 'लंबाई' या दस्तावेज़ फ़ाइलों पर दे सकता है कि कितने पेज हैं .. आदि आदि ..

उत्तर

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

मैं अपने दो सवाल लगता है बल्कि असंबंधित हैं। –

4

रजिस्ट्री से सीधे इस तरह की चीजें पढ़ना आम तौर पर एक बुरा विचार है (सभी gory details के लिए Raymond Chen's blog देखें)। इस विशेष मामले में, आप जिस एपीआई को चाहते हैं AssocQueryStringshlwapi.h में है।

यहाँ सी ++ कोड है:

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

आप सी # से या तो एक अच्छा नेट के अनुकूल एपीआई उजागर C++/CLI के माध्यम से इस का उपयोग कर सकते हैं; या सीधे 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

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 
     } 



    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे