我正在尝试使用c#替换(或添加不存在的情况下).exe文件中的图标。
到目前为止,我得到了这个:
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< - 我通过使用Resource Hacker打开test.exe来获取此数据。
我确实得到消息框“已更新”,如果我在资源黑客中打开生成的exe,资源会被替换,但图标不会出现,它只是空的。此代码也不会替换,资源中的类型“Icon”,它将删除所有内容并添加“Icon”,如果我使用BeginUpdateResource(path, false );它不会取代它,但它会添加另一个“图标”。
在哪里可以找到使用c#替换/添加图标的示例,忽略资源用于图标的名称,或者资源是否不存在?