2012-03-07 196 views
3

我想在我的C#應用​​程序中獲取文件類型的名稱,即在Windows的文件屬性中顯示...例如.log文件的類型爲LOG file (.log).batBatch file for Windows (.bat)(從我的lang翻譯過來,所以可能不會準確)。如何獲取文件類型名稱?

請問,我在哪裏可以找到此信息?或如何得到這個? 我發現Get-Registered-File-Types-and-Their-Associated-Ico文章,其中的autor顯示如何獲得圖標,但不是操作系統中顯示的文件的類型名稱。

+0

見鏈接http://stackoverflow.com/questions/770023/how-do-i-get-file-type-in​​formation-based-on-extention-not-mime-in-c-sharp - 你需要FriendlyDocName – 2012-03-07 12:14:00

+1

請理解,永遠不會擴展將有這個信息。使用該應用程序的第一個應用程序必須在註冊表中註冊此信息。 – 2012-03-07 12:27:59

回答

0

您可以從註冊表中讀取信息

使用像GetDescription("cpp")GetDescription(".xml")

public static string ReadDefaultValue(string regKey) 
{ 
    using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(regKey, false)) 
    { 
     if (key != null) 
     { 
      return key.GetValue("") as string; 
     } 
    } 
    return null; 
} 

public static string GetDescription(string ext) 
{ 
    if (ext.StartsWith(".") && ext.Length > 1) ext = ext.Substring(1); 

    var retVal = ReadDefaultValue(ext + "file"); 
    if (!String.IsNullOrEmpty(retVal)) return retVal; 


    using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("." + ext, false)) 
    { 
     if (key == null) return ""; 

     using (var subkey = key.OpenSubKey("OpenWithProgids")) 
     { 
      if (subkey == null) return ""; 

      var names = subkey.GetValueNames(); 
      if (names == null || names.Length == 0) return ""; 

      foreach (var name in names) 
      { 
       retVal = ReadDefaultValue(name); 
       if (!String.IsNullOrEmpty(retVal)) return retVal; 
      } 
     } 
    } 

    return ""; 
} 
+0

+1這啓發了我一個小的修改版本,謝謝:) – Zavael 2012-04-16 11:41:03

1

您可以使用SHGetFileInfo獲取此信息。

using System; 
using System.Runtime.InteropServices; 

namespace GetFileTypeAndDescription 
{ 

class Class1 
{ 
[STAThread] 
static void Main(string[] args) 
{ 
SHFILEINFO shinfo = new SHFILEINFO(); 
IntPtr i = Win32.SHGetFileInfo(@"d:\temp\test.xls", 0, ref 
shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_TY PENAME); 
string s = Convert.ToString(shinfo.szTypeName.Trim()); 
Console.WriteLine(s); 
} 
} 

[StructLayout(LayoutKind.Sequential)] 
public struct SHFILEINFO 
{ 
public IntPtr hIcon; 
public IntPtr iIcon; 
public uint dwAttributes; 
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
public string szDisplayName; 
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 
public string szTypeName; 
}; 

class Win32 
{ 
public const uint SHGFI_DISPLAYNAME = 0x00000200; 
public const uint SHGFI_TYPENAME = 0x400; 
public const uint SHGFI_ICON = 0x100; 
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon 
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon 

[DllImport("shell32.dll")] 
public static extern IntPtr SHGetFileInfo(string pszPath, uint 
dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); 
} 
} 
3

您必須調用相應的Shell函數SHGetFileInfo,這是一個本機Win32 API。

class NativeMethods 
{ 
    private const int FILE_ATTRIBUTE_NORMAL = 0x80; 
    private const int SHGFI_TYPENAME = 0x400; 

    [DllImport("shell32.dll", CharSet = CharSet.Unicode)] 
    private static extern IntPtr SHGetFileInfo(
     string pszPath, 
     int dwFileAttributes, 
     ref SHFILEINFO shinfo, 
     uint cbfileInfo, 
     int uFlags); 


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 
    private struct SHFILEINFO 
    { 
     public SHFILEINFO(bool b) 
     { 
      hIcon = IntPtr.Zero; 
      iIcon = 0; 
      dwAttributes = 0; 
      szDisplayName = ""; 
      szTypeName = ""; 
     } 

     public IntPtr hIcon; 
     public int iIcon; 
     public uint dwAttributes; 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
     public string szDisplayName; 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 
     public string szTypeName; 
    }; 


    public static string GetShellFileType(string fileName) 
    { 
     var shinfo = new SHFILEINFO(true); 
     const int flags = SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES; 

     if (SHGetFileInfo(fileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags) == IntPtr.Zero) 
     { 
      return "File"; 
     } 

     return shinfo.szTypeName; 
    } 
} 

然後,只需撥打NativeMethods.GetShellFileType("...")

0

你必須使用的SHGetFileInfo,見下面的鏈接,一些代碼:

http://www.pinvoke.net/default.aspx/shell32.shgetfileinfo

+1

這隻顯示如何檢索圖標,而不是文件類型名稱。雖然額外的努力是微不足道的,但如果你真的顯示要做什麼,而不是僅僅發佈鏈接,這將是有幫助的。 – 2012-03-07 12:03:51