Changing the icon of EXE files programatically  
Author Message
ciM2pHat4U





PostPosted: Visual C# General, Changing the icon of EXE files programatically Top

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


Visual C#5  
 
 
nobugz





PostPosted: Visual C# General, Changing the icon of EXE files programatically Top

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...


 
 
ciM2pHat4U





PostPosted: Visual C# General, Changing the icon of EXE files programatically Top

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

 
 
nobugz





PostPosted: Visual C# General, Changing the icon of EXE files programatically Top

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

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.Description = "nobugz was here";


// End optional code
lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System) + "cmd.exe", 1);
lnk.Save(null);
}



 
 
ciM2pHat4U





PostPosted: Visual C# General, Changing the icon of EXE files programatically Top

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.