Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change the icon of the executable

I am developing an application called WeatherBar. Its main functionality is based on its interaction with the Windows 7 taskbar — it changes the icon depending on the weather conditions in a specific location.

The icons I am using in the application are all stored in a compiled native resource file (.res) — I am using it instead of the embedded resource manifest for icons only. By default, I modify the Icon property of the main form to change the icons accordingly and it works fine, as long as the icon is not pinned to the taskbar. When it gets pinned, the icon in the taskbar automatically switches to the default one for the executable (with index 0 in the resource file).

After doing a little bit of research, I figured that a way to change the icon would be changing the shortcut icon (as all pinned applications are actually shortcuts stored in the user folder). But it didn't work.

I assume that I need to change the icon for the executable, and therefore use UpdateResource, but I am not entirely sure about this. My executable is not digitally signed, so it shouldn't be an issue modifying it.

What would be the way to solve this issue?

like image 689
Den Delimarsky Avatar asked Mar 29 '10 15:03

Den Delimarsky


People also ask

How do I change the icon of a .EXE file in Windows 7?

Right-click the original EXE file (not the copy you made) and choose “Open using Resource Hacker.” In the Resource Hacker window, select the “Icon” folder in the left pane. Click the “Action” menu and then select “Replace Icon.”


2 Answers

If you want to do this programatically, I would start by looking at the Portable Executable file format (Wikipedia entry). The resources section (.rsrc, see section 6.9) should contain the icon. Using this information, you can write a tool to modify the icon.

If you just want to quickly change an icon in an existing file, you might be able to hack it up in the Visual Studio resource editor. I tested this with a file by deleting the old icon and adding a new one. The .exe icon changed in Explorer to the new icon, and the new icon appeared on the Start menu when I dragged it there.

-- Edit --

Yes, I agree that using UpdateResource is a good approach. Here is an example I found of using C++ functions to do so, and a P/Invoke signature for UpdateResource and FindResource.

like image 77
Paul Williams Avatar answered Oct 22 '22 03:10

Paul Williams


 private void button1_Click(object sender, EventArgs e)
    {
      String path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
      String name = "test";
      Shell32.Shell shl = new Shell32.ShellClass();
      // Optional code to create the shortcut
      System.IO.StreamWriter sw = new System.IO.StreamWriter(path + @"\" + name + ".lnk", false);
      sw.Close();
      // End optional code
      Shell32.Folder dir = shl.NameSpace(path);
      Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");
      Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
      // Optional code to create the shortcut
      lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System)
+ @"\notepad.exe";
      lnk.Description = "nobugz was here";
      lnk.Arguments = @"c:\sample.txt";
      lnk.WorkingDirectory = @"c:\";
      // End optional code
      lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System)
+ "cmd.exe", 1);
      lnk.Save(null);
    }

This was taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/

It may help.

like image 34
Art W Avatar answered Oct 22 '22 03:10

Art W