Get FileType

  • Thread starter Thread starter SimpleMan75
  • Start date Start date
S

SimpleMan75

Using System.IO, is there any way, using FileInfo or any other way, to tell
what filetype the file is?

In other words, if I have a group of files, and I want to do something with
only .doc file, can I do this using the System.IO class, or am I just stuck
with checking the last three characters of the filename (with substring)?
 
Using System.IO, is there any way, using FileInfo or any other way, to
tell what filetype the file is?

In other words, if I have a group of files, and I want to do something
with only .doc file, can I do this using the System.IO class, or am I just
stuck with checking the last three characters of the filename (with
substring)?

If you're willing to use the API you can do:

public static string GetFileType(string strFilename)
{
SHFILEINFO shfi = new SHFILEINFO();
string strFileType = "";
uint uFlags = (uint)(SHGFI.SHGFI_TYPENAME);

SHGetFileInfo(strFilename, 0, out shfi, Marshal.SizeOf(shfi), uFlags);
strFileType = shfi.szTypeName;

if (shfi.hIcon != IntPtr.Zero)
DestroyIcon(shfi.hIcon);

return strFileType;
}

You would need to declare SHFILEINFO and do a prototype for SHGetFileInfo.
 
Here is the declaration for SHFILEINFO and SHGetFileInfo

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

}

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SHGetFileInfo(string pszPath, int
dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, int uFlags);

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
 
Back
Top