Getting file description

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

Guest

Hi everybody,

I need some help in a .NET application. I need to get some files'
properties, which cannot be found using the FileInfo class - especially the
file description (which you can find with right click - Properties in Windows
Explorer).

I guess that I have no choice but use dll import, but I don't know which
function to use. I found the GetFileInformationByHandleEx function from
Windows API, but this applies only to Vista and my application will be
installed on other OSs too.
 
I guess that I have no choice but use dll import, but I don't know which
function to use. I found the GetFileInformationByHandleEx function from
Windows API, but this applies only to Vista and my application will be
installed on other OSs too.

You can get a lot from SHGetFileInfo e.g.:

/// <summary>
/// Uses the Windows API to get a file type
/// </summary>
/// <param name="filename">Full path to file</param>
/// <returns>File type as a string</returns>
public static string GetFileType(string filename)
{
SHFILEINFO shfi = new SHFILEINFO();
string fileType = "";
uint uFlags = (uint)(SHGFI.SHGFI_TYPENAME);

SHGetFileInfo(filename, 0, out shfi, Marshal.SizeOf(shfi), uFlags);
fileType = shfi.szTypeName;

//If SHGetFileInfo returns an icon handle in the hIcon member of the
SHFILEINFO structure
//pointed to by psfi, you are responsible for freeing it with DestroyIcon
when you no longer need it.
if (shfi.hIcon != IntPtr.Zero)
DestroyIcon(shfi.hIcon);

return fileType;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public 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;
}

[DllImport("shell32.dll")]
public static extern int SHGetFileInfo(string pszPath, int
dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, int uFlags);

[DllImport("user32.dll", EntryPoint="DestroyIcon", SetLastError=true,
CallingConvention= CallingConvention.StdCall)]
public static extern int DestroyIcon(IntPtr hIcon);


Have a look at SHGetFileInfo in the (unfiltered) help, it may do all you
need.
 
Hi Jeff,

Thanks for your reply. Unfortunately, SHGetFileInfo applies to Win CE and I
need to get the file description in a desktop OS.

Maybe I wasn't very clear, but by other OSs I meant other than Vista (e.g.
XP, 2000).

Any other ideas would be great. Thanks anyway,

--
Mihai Tătăran
Director,
H.P.C. Consulting
http://www.hpc-consulting.ro


Jeff Gaines said:
I guess that I have no choice but use dll import, but I don't know which
function to use. I found the GetFileInformationByHandleEx function from
Windows API, but this applies only to Vista and my application will be
installed on other OSs too.

You can get a lot from SHGetFileInfo e.g.:

/// <summary>
/// Uses the Windows API to get a file type
/// </summary>
/// <param name="filename">Full path to file</param>
/// <returns>File type as a string</returns>
public static string GetFileType(string filename)
{
SHFILEINFO shfi = new SHFILEINFO();
string fileType = "";
uint uFlags = (uint)(SHGFI.SHGFI_TYPENAME);

SHGetFileInfo(filename, 0, out shfi, Marshal.SizeOf(shfi), uFlags);
fileType = shfi.szTypeName;

//If SHGetFileInfo returns an icon handle in the hIcon member of the
SHFILEINFO structure
//pointed to by psfi, you are responsible for freeing it with DestroyIcon
when you no longer need it.
if (shfi.hIcon != IntPtr.Zero)
DestroyIcon(shfi.hIcon);

return fileType;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public 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;
}

[DllImport("shell32.dll")]
public static extern int SHGetFileInfo(string pszPath, int
dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, int uFlags);

[DllImport("user32.dll", EntryPoint="DestroyIcon", SetLastError=true,
CallingConvention= CallingConvention.StdCall)]
public static extern int DestroyIcon(IntPtr hIcon);


Have a look at SHGetFileInfo in the (unfiltered) help, it may do all you
need.
 
Thanks for your reply. Unfortunately, SHGetFileInfo applies to Win CE and I
need to get the file description in a desktop OS.

Maybe I wasn't very clear, but by other OSs I meant other than Vista (e.g.
XP, 2000).

It works in anything from NT4 onwards - it's the help that is confusing.
Look in the bottom right pane and you should see a choice of Windows CE or
Windows Shell, it's the latter you want, it's part of the API.
 
Hi Jeff,

Sorry for my mistake - the function can indeed be used for desktop OSs.

Unfortunatelly, the function you pointed out did not help me, but in the
meantime I found what I was looking for. Please remember that I needed some
files' description, which can easily be found out using the
System.Diagnostics.FileVersionInfo class. Here is some code:

string file = "bla bla";
string[] systemDirs = Environment.GetEnvironmentVariable("PATH").Split(new
char[] { ';' });
foreach (string dir in systemDirs)
{
if (File.Exists(dir + "\\" + file))
{
FileVersionInfo fileCodec = FileVersionInfo.GetVersionInfo(dir + "\\"
+ file);
if (fileCodec.FileDescription != null && fileCodec.FileDescription !=
"")
{
// do something here
}
}
}

Anyway, thanks!
 
Back
Top