Getting file description
am 08.11.2007 13:39:01 von MihaiT
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.
--
Mihai TÄtÄran
Director,
H.P.C. Consulting
http://www.hpc-consulting.ro
Re: Getting file description
am 08.11.2007 16:08:43 von Jeff Gaines
On 08/11/2007 in message
MihaiT wrote:
>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.:
///
/// Uses the Windows API to get a file type
///
/// Full path to file
/// File type as a string
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.
--
Jeff Gaines
Re: Getting file description
am 08.11.2007 19:41:04 von MihaiT
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" wrote:
> On 08/11/2007 in message
> MihaiT wrote:
>
> >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.:
>
> ///
> /// Uses the Windows API to get a file type
> ///
> /// Full path to file
> /// File type as a string
> 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.
>
> --
> Jeff Gaines
>
Re: Getting file description
am 08.11.2007 21:53:52 von Jeff Gaines
On 08/11/2007 in message
<5A0C2BE2-20C2-4500-8590-F7761F851DDF@microsoft.com> MihaiT wrote:
>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.
--
Jeff Gaines
Re: Getting file description
am 09.11.2007 10:54:00 von MihaiT
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!
--
Mihai TÄtÄran
"Jeff Gaines" wrote:
> On 08/11/2007 in message
> <5A0C2BE2-20C2-4500-8590-F7761F851DDF@microsoft.com> MihaiT wrote:
>
> >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.
>
> --
> Jeff Gaines
>