Tags: changing, example, exe, familiar, files, icon, languages, microsoft, msdn, net, programatically, software, visual

Changing the icon of EXE files programatically

On Microsoft » Microsoft Visual C#

3,491 words with 4 Comments; publish: Sat, 22 Dec 2007 22:56:00 GMT; (30078.13, « »)

Can anyone show me an example of how to change the icon of an EXE programatically with C# (I am not too familiar with the other .net languages)? For example, how could I get the icon of calc.exe and set that as the icon of notepad.exe? I don't know if C# has any built in classes that address this, probably not because I have searched all day. In that case, I guess I could read calc.exe byte by byte, save the parts that describe the embedded icon, and write that to notepad.exe byte by byte in the correct places. Is anyone familiar with the structure of a windows EXE and how/where exactly the icon information is stored in the file?

Thanks in advance

All Comments

Leave a comment...

  • 4 Comments
    • That's not a trivial undertaking, you'll have to modify the resource table in the .exe file. The program's icon is the first icon in the table. You'll run into major problems if the new icon is larger than the old icon. Perhaps this is one of the reasons that such a utility is not widely available.

      Perhaps a better approach is the modify the shortcut for the program. You could use the ShellLinkObject COM class exported by shell32.dll...

      #1; Thu, 30 Aug 2007 12:34:00 GMT
    • Hmm I guess modifying the shortcut will be okay for in my case, although I am still interested in modifying the EXE just for learning purposes. How do I export or use the ShellLinkObject that you mentioned?
      #2; Thu, 30 Aug 2007 12:35:00 GMT
    • Well,

      that took me all morning to figure out. I don't think anybody

      knows how to create a new shortcut on the desktop, other than using

      scripting. I found a way though. To use this code,

      right-click your project and choose Add Reference, Browse tab,

      c:\windows\system32\shell32.dll; that gives your program access to the

      ShellLinkObject COM object.

      Here's sample code to set the icon for a shortcut. I've also

      included the code to create a completely new shortcut. The

      shortcut is created on the user's desktop, starts Notepad and uses the

      icon from the command shell:

      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 + .visual-csharp.todaysummary.com."\" + 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) + .visual-csharp.todaysummary.com."\notepad.exe";

      lnk.Description = "nobugz was here";

      lnk.Arguments = .visual-csharp.todaysummary.com."c:\sample.txt";

      lnk.WorkingDirectory = .visual-csharp.todaysummary.com."c:\";

      // End optional code

      lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System) + "cmd.exe", 1);

      lnk.Save(null);

      }

      #3; Thu, 30 Aug 2007 12:36:00 GMT
    • Cooool, that looks intuitive enough I'll give it a try when I get the chance. I really appreciate your help nobugz, thanks! If only the original problem was as straightforward.
      #4; Thu, 30 Aug 2007 12:37:00 GMT