473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

SHGetFileInfo - Getting blank file type

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
Aug 2 '06 #1
4 9080
PAGates,

Without seeing your declaration of SHGetFileInfo, its difficult to tell.
Post the declarations for the function and the structure.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:DD**********************************@microsof t.com...
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


Aug 2 '06 #2
// 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();
}
}
}

Aug 2 '06 #3
[DllImport("shell32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint .....

Willy.

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
| // 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();
| }
| }
| }
|
Aug 2 '06 #4
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 [MVP]" wrote:
[DllImport("shell32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint .....

Willy.

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
| // 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();
| }
| }
| }
|
Aug 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CLEAR-RCIC | last post by:
In the old days, you could get a file type in ASP by doing: dim fs, fld, f set fs = server.createobject("Scripting.FileSystemObject") set fld = fs.GetFolder(server.mappath(sCurrentDir)) for...
2
by: Brian Henry | last post by:
I want to list out a directory listing along with showing the file type name (like explorer does when it says something like "MyDoc.DOC - Microsoft Word Document" How do I get that file type name...
3
by: BartlebyScrivener | last post by:
Using Python on Windows XP, I am able to get almost all file and path info using os.path or stat, but I don't see a way to retrieve the file type? E.g. Microsoft Word file, HTML file, etc, the...
7
by: Nathan Sokalski | last post by:
I would like to be able to get the ContentType of a file programmatically (for example, I want *.txt files to return "text/plain"). I could not find a way to do this using VB.NET's classes, but I...
5
by: mike | last post by:
Hi, I have been playing with VB.NET/C# for getting some general properties of a fileinfo object. However, FileInfo object does not seem to expose some of the basic properties like TYPE that used...
4
by: Sin Jeong-hun | last post by:
I already found that I have to use SHGetFileInfo to get the System's associated icon with that file. But what about I just want to get associated icon for some specific extensions? For example,...
0
Blade
by: Blade | last post by:
Hello Friends well this is my first post on this site, hope i get the solution for my problem During the development of one project i am facing this problem i use a SHGetFileInfo structure to...
2
by: jack | last post by:
Hi, Im creating a program in which im trying display a file list in the listview. similar as windows explorer detailed view. im able to get every thing except the file type. im stuck here and...
9
hsriat
by: hsriat | last post by:
How can I add my validation script to this file upload code? Validation Script:function validate(frm) { if (!/(\.(gif|jpg|jpeg|png))$/i.test(frm.file.value)){ alert('Invalid file...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.