Extract Icons from the Registry by their Extensions

By erik little

A program that extracts icons from the registry by their associated extensions.

One of my biggest needs that i find myself looking for is icons for other applications. So I have made a little program that will traverse the local registry and load all of the extensions into a listbox. When a selection is made if there is an icon for the selected extension it will display in a pictureBox along with some other important attributes.


There is a save icon button that does not save the icon properly; however, I will be updating this code over the next two weeks to take care of that problem.

If anyone right off hand knows how to save an icon by using the built-in Icon.Save method please let me know.


--Here is a quick glance of the GetIcon method-- The rest of the code is packed neatly in the link provided..

----GetIcon

----Win32---->
 [DllImport("shell32.dll")]
        extern static IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);

        //If the function succeeds, the return value is nonzero.
        //If the function fails, the return value is zero.
        [DllImport("user32.dll")]
        extern static bool DestroyIcon(IntPtr hIcon);

<----

 


        private void GetIcon()
        {

            this.txBxDllExePath.Text = string.Empty;
            this.txBxIconIndex.Text = string.Empty;

            string strIconPath = string.Empty;//Path to the .dll or .exe
            int nIcon = 0; //The icon number (index) within a file
            IntPtr hIcon = IntPtr.Zero; //The "Icon Handle" for the default icon

            RegistryKey regKeyExt = null;
            object regKeyExt_Val = null;
            RegistryKey regKeyExtEx = null;
            RegistryKey regKeyDefaultIcon = null;
            object oIconPath = null;
            string[] path_IconNum = null;
            Icon theIcon = null;
            bool icoDestroy = false;

            try
            {
                //1.
                regKeyExt = Registry.ClassesRoot.OpenSubKey(this.txBxIconFile.Text != string.Empty ? this.txBxIconFile.Text : ".txt");
                regKeyExt_Val = regKeyExt.GetValue(string.Empty);//GetValue by empty string returns the (Default) Value
                //------------------------------

                //2.
                regKeyExtEx = Registry.ClassesRoot.OpenSubKey(regKeyExt_Val.ToString());
                regKeyDefaultIcon = regKeyExtEx.OpenSubKey("DefaultIcon");
                //Example:
                //%SystemRoot%\system32\shell32.dll,-152
                oIconPath = regKeyDefaultIcon.GetValue(string.Empty);

                //Split the Path and Icon
                path_IconNum = oIconPath.ToString().Split(',');

                //Get icon Path from string array
                strIconPath = path_IconNum[0]; this.txBxDllExePath.Text = path_IconNum[0];

                //Get icon Number from string array
                if (int.TryParse(path_IconNum[1], out nIcon))
                {
                    //Display Icon  Index (Index inside the .dll or .exe which it resides)
                    this.txBxIconIndex.Text = nIcon.ToString();
                    //------------------------------

                    //3.
                    //Win32
                    //Extract the icon handle
                    hIcon = ExtractIcon(Process.GetCurrentProcess().Handle, strIconPath, nIcon);

                    //Get icon form .dll or .exe
                    if (hIcon == IntPtr.Zero) return;
                    theIcon = Icon.FromHandle(hIcon);

                    if (theIcon == null)
                    {
                        //Win32
                        icoDestroy = DestroyIcon(hIcon);
                        goto cleanUp;
                    }
                    this.pbIcon.Image = theIcon.ToBitmap();
                    this.Icon = theIcon;
                    //Win32
                    icoDestroy = DestroyIcon(hIcon);
                }
            }
            catch (Exception ex)
            {
                this.Icon = Properties.Resources.zoom_in_copy;
                this.pbIcon.Image = Properties.Resources.zoom_in_copy.ToBitmap();
                this.txBxIconFile.Text = string.Empty;
                MessageBox.Show(ex.Message);
            }

        cleanUp:
            {
                strIconPath = string.Empty;//Path to the .dll or .exe
                nIcon = 0; //The icon number (index) within a file
                hIcon = IntPtr.Zero; //The "Icon Handle" for the default icon

                regKeyExt_Val = null;

                if (regKeyExtEx != null) regKeyExtEx.Close();
                if (regKeyDefaultIcon != null) regKeyDefaultIcon.Close();
                if (regKeyExt != null) regKeyExt.Close();
                if (theIcon != null) theIcon.Dispose();
                //Win32
                if (!icoDestroy)
                    icoDestroy = DestroyIcon(hIcon);

                oIconPath = null;
                theIcon = null;
            }

        }

-----------------------

http://www.eggheadcafe.com/fileupload/215296388_Get-Default-Icons[0].zip 

Popularity  (1294 Views)
Picture
Biography - erik little
Building Industry.
Create New Account
Article Discussion: Extract Icons from the Registry by their Extentions
erik little posted at Monday, December 25, 2006 7:04 PM