Jump to content
Sign in to follow this  
CyberSlug

Obtain icon associated with file type

Recommended Posts

I would like to display the icon associated with a file (based on file extension. My code works for ".au3" but NOT for most extensions like ".txt"

The registry key I get for txtfile is "%SystemRoot%\system32\shell32.dll,-152"

I have two issues--the second issue being a show stopper :lmao:

1) The %SystemRoot% appears not to be automatically expanded. Why not??? Is it because $iconFile results from a function call instead of a literal string assignment? This issue causes the FileExists check to fail... Sure I could manually call EnvGet, but I'm lazy,

2) The -152 is an Icon ID instead of and Icon Index.... (Microsoft documentation says that the minus sign indicates the value is to be treated as ID#152.) I need the index for GuiCtrlCreateIcon... could GuiCtrlCreateIcon be modified to support icon IDs?

By the way,DllCall(... "SHGetFileInfo "...) will not work due to the structures required by the function.

Here's my code:

$filename = "SomeFile.txt" ;TRY ALSO "SomeFile.au3"
$extension = StringTrimLeft($filename, StringInStr($filename,".",1,-1))

$iconFile = RegRead("HKCR\" & RegRead("HKCR\."&$extension,"") & "\DefaultIcon", "")
$iconID = 0

If StringInStr($iconFile, ",") Then
    $iconID = StringTrimLeft($iconFile, StringInStr($iconFile,","))
    $iconFile = StringLeft($iconFile, StringInStr($iconFile,",")-1)
EndIf

Opt("ExpandEnvStrings",1);registry keys might contain %sytemroot% in path
MsgBox(4096, "Not expanded???", $iconFile)
MsgBox(4096,"Expanded","%SystemRoot%\system32\shell32.dll")

If Not FileExists($iconFile) Then
    MsgBox(4096,"Error", "Icon resource not found.... quitting")
    Exit
EndIf

; Show icon in GUI
GuiSetState(@SW_SHOW, GuiCreate("Icon", 100, 100))
GuiCtrlCreateIcon($iconFile, $iconID, 30, 30, 32, 32)
sleep(3000)

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Well, maybe "txt" is one of the few where I should do a special case (use index of 70 on Windows XP....

If I remove the FileExists check, "bmp" and "wav" and "doc" all seem to work :lmao:

Edit: Firefox also complicates things by putting the icon info in something like

RegRead("HKEY_CLASSES_ROOT\CLSID\" & RegRead("KHCR\" & RegRead("HKCR\."&$extension,"") & "\ShellEx\IconHandler","") & "\Old Icon\FirefoxHTML\DefaultIcon", "")

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

@CyberSlug:

I was in the same boat. I had those issues too.

A while ago I made an application launcher that retrieved icons.

Let me see how I did it back then...

Edit:

Because of these issues I gave up and added FileOpenDialogs.

I'm sorry, I can't help you.

Edited by SlimShady

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...