SHGetFileInfo - Getting blank file type

G

Guest

Hello All,

Before I post a console app that demonstrates this problem, can anybody see
any problems with what I am doing below?

private string GetFileType(string Path)
{
SHFILEINFO shfi = new SHFILEINFO();
int cbFileInfo = Marshal.SizeOf(shfi);
uint dwflag = (uint) (SHGFI.SHGFI_TYPENAME |
SHGFI.SHGFI_USEFILEATTRIBUTES);
uint dwAttr = FILE_ATTRIBUTE_NORMAL;
IntPtr hr = SHGetFileInfo(Path, dwAttr, ref shfi, (uint)
Marshal.SizeOf(shfi), dwflag);
return shfi.szTypeName;
}

I always get a blank szTypeName.

Note: If I add the SHGFI.SHGFI_DISPLAYNAME flag, I get the first letter of
the name in the szDisplayName field.

It's probably something obvious, but not to me! : )

Thanks,
PAGates
 
N

Nicholas Paldino [.NET/C# MVP]

PAGates,

Without seeing your declaration of SHGetFileInfo, its difficult to tell.
Post the declarations for the function and the structure.
 
G

Guest

// Here is a Console App that demonstrates the problem
// on my system
using System;
using System.Runtime.InteropServices;

namespace GetFileType
{
class Class1
{
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;
}

public enum SHGFI
{
SHGFI_ICON = 0x000000100,
SHGFI_DISPLAYNAME = 0x000000200,
SHGFI_TYPENAME = 0x000000400,
SHGFI_ATTRIBUTES = 0x000000800,
SHGFI_ICONLOCATION = 0x000001000,
SHGFI_EXETYPE = 0x000002000,
SHGFI_SYSICONINDEX = 0x000004000,
SHGFI_LINKOVERLAY = 0x000008000,
SHGFI_SELECTED = 0x000010000,
SHGFI_ATTR_SPECIFIED = 0x000020000,
SHGFI_LARGEICON = 0x000000000,
SHGFI_SMALLICON = 0x000000001,
SHGFI_OPENICON = 0x000000002,
SHGFI_SHELLICONSIZE = 0x000000004,
SHGFI_PIDL = 0x000000008,
SHGFI_USEFILEATTRIBUTES = 0x000000010,
SHGFI_ADDOVERLAYS = 0x000000020,
SHGFI_OVERLAYINDEX = 0x000000040
}

private const int FILE_ATTRIBUTE_NORMAL = 0x80;

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

private string GetFileType(string Path)
{
try
{
SHFILEINFO shfi = new SHFILEINFO();
int cbFileInfo = Marshal.SizeOf(shfi);
uint dwflag = (uint) (SHGFI.SHGFI_TYPENAME |
SHGFI.SHGFI_USEFILEATTRIBUTES);
// uint dwflag = 0x400;// | 0x10;
uint dwAttr = FILE_ATTRIBUTE_NORMAL;
IntPtr hr = SHGetFileInfo(Path, dwAttr, ref shfi, (uint)
Marshal.SizeOf(shfi), dwflag);
// IntPtr hr = SHGetFileInfo(Path, 0, ref shfi, (uint)
Marshal.SizeOf(shfi), dwflag);
return shfi.szTypeName;
}
catch (Exception e)
{
return "ERROR: " + e.Message;
}

}

[STAThread]
static void Main(string[] args)
{
Console.Write("Please enter a file path: ");
string Path = Console.ReadLine();
Class1 NewClass = new Class1();
string FileType = NewClass.GetFileType(Path);
Console.WriteLine("The file type of your file is \"{0}\"", FileType);
Console.WriteLine("\nPress any key to continue...");
Console.ReadLine();
}
}
}
 
W

Willy Denoyette [MVP]

[DllImport("shell32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint .....



Willy.

| // Here is a Console App that demonstrates the problem
| // on my system
| using System;
| using System.Runtime.InteropServices;
|
| namespace GetFileType
| {
| class Class1
| {
| 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;
| }
|
| public enum SHGFI
| {
| SHGFI_ICON = 0x000000100,
| SHGFI_DISPLAYNAME = 0x000000200,
| SHGFI_TYPENAME = 0x000000400,
| SHGFI_ATTRIBUTES = 0x000000800,
| SHGFI_ICONLOCATION = 0x000001000,
| SHGFI_EXETYPE = 0x000002000,
| SHGFI_SYSICONINDEX = 0x000004000,
| SHGFI_LINKOVERLAY = 0x000008000,
| SHGFI_SELECTED = 0x000010000,
| SHGFI_ATTR_SPECIFIED = 0x000020000,
| SHGFI_LARGEICON = 0x000000000,
| SHGFI_SMALLICON = 0x000000001,
| SHGFI_OPENICON = 0x000000002,
| SHGFI_SHELLICONSIZE = 0x000000004,
| SHGFI_PIDL = 0x000000008,
| SHGFI_USEFILEATTRIBUTES = 0x000000010,
| SHGFI_ADDOVERLAYS = 0x000000020,
| SHGFI_OVERLAYINDEX = 0x000000040
| }
|
| private const int FILE_ATTRIBUTE_NORMAL = 0x80;
|
| [DllImport("shell32.dll", CharSet=CharSet.Auto)]
| public static extern IntPtr SHGetFileInfo(string pszPath, uint
| dwFileAttributes,
| ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
|
| private string GetFileType(string Path)
| {
| try
| {
| SHFILEINFO shfi = new SHFILEINFO();
| int cbFileInfo = Marshal.SizeOf(shfi);
| uint dwflag = (uint) (SHGFI.SHGFI_TYPENAME |
| SHGFI.SHGFI_USEFILEATTRIBUTES);
| // uint dwflag = 0x400;// | 0x10;
| uint dwAttr = FILE_ATTRIBUTE_NORMAL;
| IntPtr hr = SHGetFileInfo(Path, dwAttr, ref shfi, (uint)
| Marshal.SizeOf(shfi), dwflag);
| // IntPtr hr = SHGetFileInfo(Path, 0, ref shfi, (uint)
| Marshal.SizeOf(shfi), dwflag);
| return shfi.szTypeName;
| }
| catch (Exception e)
| {
| return "ERROR: " + e.Message;
| }
|
| }
|
| [STAThread]
| static void Main(string[] args)
| {
| Console.Write("Please enter a file path: ");
| string Path = Console.ReadLine();
| Class1 NewClass = new Class1();
| string FileType = NewClass.GetFileType(Path);
| Console.WriteLine("The file type of your file is \"{0}\"", FileType);
| Console.WriteLine("\nPress any key to continue...");
| Console.ReadLine();
| }
| }
| }
|
 
G

Guest

Thanks Willy. That fixed it.

Would you consider this a bug? It seems that CharSet.Auto should pick this
up.

Thanks again,
Paul

Willy Denoyette said:
[DllImport("shell32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint .....



Willy.

| // Here is a Console App that demonstrates the problem
| // on my system
| using System;
| using System.Runtime.InteropServices;
|
| namespace GetFileType
| {
| class Class1
| {
| 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;
| }
|
| public enum SHGFI
| {
| SHGFI_ICON = 0x000000100,
| SHGFI_DISPLAYNAME = 0x000000200,
| SHGFI_TYPENAME = 0x000000400,
| SHGFI_ATTRIBUTES = 0x000000800,
| SHGFI_ICONLOCATION = 0x000001000,
| SHGFI_EXETYPE = 0x000002000,
| SHGFI_SYSICONINDEX = 0x000004000,
| SHGFI_LINKOVERLAY = 0x000008000,
| SHGFI_SELECTED = 0x000010000,
| SHGFI_ATTR_SPECIFIED = 0x000020000,
| SHGFI_LARGEICON = 0x000000000,
| SHGFI_SMALLICON = 0x000000001,
| SHGFI_OPENICON = 0x000000002,
| SHGFI_SHELLICONSIZE = 0x000000004,
| SHGFI_PIDL = 0x000000008,
| SHGFI_USEFILEATTRIBUTES = 0x000000010,
| SHGFI_ADDOVERLAYS = 0x000000020,
| SHGFI_OVERLAYINDEX = 0x000000040
| }
|
| private const int FILE_ATTRIBUTE_NORMAL = 0x80;
|
| [DllImport("shell32.dll", CharSet=CharSet.Auto)]
| public static extern IntPtr SHGetFileInfo(string pszPath, uint
| dwFileAttributes,
| ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
|
| private string GetFileType(string Path)
| {
| try
| {
| SHFILEINFO shfi = new SHFILEINFO();
| int cbFileInfo = Marshal.SizeOf(shfi);
| uint dwflag = (uint) (SHGFI.SHGFI_TYPENAME |
| SHGFI.SHGFI_USEFILEATTRIBUTES);
| // uint dwflag = 0x400;// | 0x10;
| uint dwAttr = FILE_ATTRIBUTE_NORMAL;
| IntPtr hr = SHGetFileInfo(Path, dwAttr, ref shfi, (uint)
| Marshal.SizeOf(shfi), dwflag);
| // IntPtr hr = SHGetFileInfo(Path, 0, ref shfi, (uint)
| Marshal.SizeOf(shfi), dwflag);
| return shfi.szTypeName;
| }
| catch (Exception e)
| {
| return "ERROR: " + e.Message;
| }
|
| }
|
| [STAThread]
| static void Main(string[] args)
| {
| Console.Write("Please enter a file path: ");
| string Path = Console.ReadLine();
| Class1 NewClass = new Class1();
| string FileType = NewClass.GetFileType(Path);
| Console.WriteLine("The file type of your file is \"{0}\"", FileType);
| Console.WriteLine("\nPress any key to continue...");
| Console.ReadLine();
| }
| }
| }
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top