Replacing/Adding resources (icon) programmatically in c#?
Posted
by reverendo
on Stack Overflow
See other posts from Stack Overflow
or by reverendo
Published on 2010-06-07T23:25:05Z
Indexed on
2010/06/07
23:32 UTC
Read the original article
Hit count: 181
I am trying to replace (or add in the case it doesn't exists) icons from .exe files using c#.
So far I got this:
string filename = "c:\\test.exe";
IntPtr hResource = BeginUpdateResource(filename, true);
if (hResource.ToInt32() == 0)
throw new Exception("File Not Found");
byte[] ico = System.IO.File.ReadAllBytes("C:\\icon.ico");
IntPtr unmanagedPointer = Marshal.AllocHGlobal(ico.Length);
Marshal.Copy(ico, 0, unmanagedPointer, ico.Length);
if (UpdateResource(hResource, "Icon", "1", 1033, unmanagedPointer, Convert.ToUInt32(ico.Length)) != false)
{
MessageBox.Show("Updated");
EndUpdateResource(hResource, false);
}
Marshal.FreeHGlobal(unmanagedPointer);
"Icon", "1", 1033 <- I got this data by opening test.exe with Resource Hacker.
I do get the messagebox "Updated", and if I open the resulting exe in Resource Hacker the resource gets replaced but the icon doesn't appear, its just empty. Also that code wont replace, the type "Icon" in the resource, it will delete everything and add that "Icon" and if I use BeginUpdateResource(path, false); it will not replace it neither but it will add ANOTHER "Icon".
Where can I find an example to replace/add the icon using c# disregarding the name the resource use for the icon or if the resource doesn't exist?
© Stack Overflow or respective owner