Is there an API to get File Description/Type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Example: Take .doc or test.doc and retrieve "Microsoft Word Document". This
is the 3rd column in windows explorer when you're in detail view, and it's
named "type".

I can't find it in the FileInfo class or in the shell32.dll or the
dsofile.dll. (I must be missing it, or is there no API for this?)
 
Craig,

You can get this information using the SHGetFileInfo (like your earlier post
states). Here is an example of how to get the File Description/type that you
are asking about. You should be able to copy and paste this into a C# Console
app. Just be sure that the first parameter to the SHGetFileInfo method is a
valid file.

I hope that this helps you out!

---------------------
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_TYPENAME);
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);
}
}
 
Back
Top