Welcome to MSDN Blogs Sign in | Join | Help

What does SHGFI_USEFILEATTRIBUTES mean?

One of the flags you can pass to the SHGetFileInfo function is SHGFI_USEFILEATTRIBUTES. What does this flag mean?

It means, "Do not access the disk. Pretend that the file/directory exists, and that its file attributes are what I passed as the dwFileAttributes parameter. Do this regardless of whether it actually exists or not."

You can use this flag to get the icon for a file type. For example, to get the icon for text files, pass a filename of "x.txt", pass SHGFI_USEFILEATTRIBUTES in the uFlags, and specify FILE_ATTRIBUTE_NORMAL as the file attributes.

Why is the filename "x.txt"? Because you want it to have the extension ".txt", and you want it to be a valid filename. (Don't pass illegal filenames like "???.txt".)

Why are the file attributes FILE_ATTRIBUTE_NORMAL? Because you want SHGetFileInfo to pretend that it is a normal file. (If you passed, say, FILE_ATTRIBUTE_DIRECTORY, then you would have gotten the folder icon back, since you told the function to pretend that "x.txt" was a directory.)

Note that since you are telling SHGetFileInfo to "pretend", there are some things you lose by the nature of make-believe.

For example, if the extension is a filetype whose icon changes depending on the contents of the file, then the dynamic icon will not be returned since there is no file. You told the function to "pretend", after all, and a pretend file has no contents.

Published Tuesday, June 01, 2004 7:00 AM by oldnewthing
Filed under:

Comments

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 7:42 AM by Jack Mathews
This was so handy for a program where I needed to enumerate all of the file types in the system way back when.

Just go through .* in HKCR, get the icon for that type, then map them to their proper names and display them. I was so happy the day I found this function :)

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 7:51 AM by Henk Devos
Are you sure that you can't pass "???.txt"?
I would usually pass "*.txt" to this function and that seems to work.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 7:57 AM by njkayaker
Too bad there isn't a way of doing something similar to get the executable used to edit the file (sort of like FindExecutable).

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 7:58 AM by njkayaker
Then, again, maybe AssocQueryString would work for that.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 8:12 AM by Raymond Chen
Henk: It may work today but it won't work in Longhorn. The first parameter is supposed to be a valid filename.

njkayaker: Yes I believe a variation on AssocQueryString will do the trick.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 8:25 AM by J. Edward Sanchez
How about just ".txt"? It's a valid filename, after all. Even though Explorer won't let you create it, Explorer will correctly recognize it as having the ".txt" extension (and a blank name).

If it works, then you've saved yourself a whopping full character of unnecessary bloat!

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 8:50 AM by Len Weaver
Hey Ray, could you give us an example of a filetype whose icon changes depending upon its contents? I've never heard of such a thing before. Thanks.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 8:52 AM by Raymond Chen
Sure you have - you just don't realize it. *.lnk files for example. *.exe for another. *ico for a third.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 9:02 AM by Henk Devos
Good that you clarified that.
I don't remember where i got the "*.txt", but i certainly didn't invent this myself, and i think it is widely used like this.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 9:11 AM by Centaur
Some time ago, in times before XP and Me, there was a “tip” that had you set your icon for .bmp to %1. It gave you a slowdown on directories that contained many bitmaps, but you got to know just by looking what a picture was about.

# re: What does SHGFI_USEFILEATTRIBUTES mean?

Tuesday, June 01, 2004 4:40 PM by Mike Dunn
Setting the icon to %1 only works if there is an icon handler shell extension registered for that file type.

# Getting the icon for a given file extension

Sunday, February 24, 2008 10:54 AM by Flawless Code

Getting the icon for a given file extension

New Comments to this post are disabled
 
Page view tracker