如何获取文件或文件夹的类型?

时间:2016-05-05 10:35:39

标签: c# asp.net

我在列表框中显示一些包含所有内容和子目录的目录。现在我想获得另一个列表框中每个项目的项目类型,大小,修改时间。我成功地显示了大小和修改时间但是我如何显示类型,例如它是文件夹/文本文件/ Pdf文件还是图像?这是在Windows资源管理器详细信息视图中,我们可以在其中查看文件的类型,大小和修改时间。我怎么能这样做?

这是我的大小和上次修改时间的代码:

private void ShowFilesIn(string dir)
{
    DirectoryInfo dirInfo = new DirectoryInfo(dir);

    lstFiles.Items.Clear();
    list_size.Items.Clear();
    list_modified.Items.Clear();
    list_type.Items.Clear();
    string downloadlink;        

    foreach (FileInfo fileItem in dirInfo.GetFiles())
    {
        downloadlink = fileItem.Directory + "/" + fileItem.Name ;
        ListItem li = new ListItem();
        li.Text = fileItem.Name;
        li.Value = downloadlink;
        lstFiles.Items.Add(li);
        list_size.Items.Add(fileItem.Length.ToString());
        list_type.Items.Add(Path.GetExtension(fileItem.Name));
        list_modified.Items.Add(fileItem.LastWriteTime.ToString());
     }
}

GetExtension()方法显示扩展名,但我想要Windows资源管理器中显示的文件类型,例如:文件夹,JPEG图像,文本文件等。

2 个答案:

答案 0 :(得分:2)

您可以使用SHFILEINFO

获取所有信息

这是您可以在c#中获取FileType Info或文件夹的方法。

string filePath = .... // your File Path
FileAttributes attr = File.GetAttributes(filePath);

//detect whether its a directory or file
if ((attr & FileAttributes.Directory) != FileAttributes.Directory)
{
   SHFILEINFO shinfo = new SHFILEINFO();
   IntPtr intptr = Win32.SHGetFileInfo(fullpath, 0, ref shinfo,    (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_TYPENAME);
   var typeName = Convert.ToString(shinfo.szTypeName.Trim());
}
else 
{
  // it is Folder
}


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

 [Flags]
 enum FileInfoFlags : int
 {
     SHGFI_ICON = 0x000000100,     // get icon
     SHGFI_DISPLAYNAME = 0x000000200,     // get display name
     SHGFI_TYPENAME = 0x000000400,     // get type name
     SHGFI_ATTRIBUTES = 0x000000800,     // get attributes
     SHGFI_ICONLOCATION = 0x000001000,     // get icon location
     SHGFI_EXETYPE = 0x000002000,     // return exe type
     SHGFI_SYSICONINDEX = 0x000004000,     // get system icon index
     SHGFI_LINKOVERLAY = 0x000008000,     // put a link overlay on icon
     SHGFI_SELECTED = 0x000010000,     // show icon in selected state
     SHGFI_ATTR_SPECIFIED = 0x000020000,     // get only specified attribtes
     SHGFI_LARGEICON = 0x000000000,     // get large icon
     SHGFI_SMALLICON = 0x000000001,     // get small icon
     SHGFI_OPENICON = 0x000000002,     // get open icon
     SHGFI_SHELLICONSIZE = 0x000000004,     // get shell size icon
     SHGFI_PIDL = 0x000000008,     // pszPath is a pidl
     SHGFI_USEFILEATTRIBUTES = 0x000000010,     // use passed dwFileAttribute
     SHGFI_ADDOVERLAYS = 0x000000020,     // apply the appropriate overlays
     SHGFI_OVERLAYINDEX = 0x000000040,     // Get the index of the overlay in 
     // the upper 8 bits of the iIcon
 }

 public 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);
}

答案 1 :(得分:0)

您可以使用GetMimeMapping从其路径中获取每个文件的MimeType