在 C# 中以编程方式替换/添加资源(图标)?
我正在尝试使用 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 中打开生成的 exe,资源将被替换,但图标不会出现,它只是空的。另外,该代码不会替换资源中的“图标”类型,它将删除所有内容并添加该“图标”,如果我使用 BeginUpdateResource(path, false);它也不会取代它,但会添加另一个“图标”。
我在哪里可以找到使用 c# 替换/添加图标的示例,忽略资源用于图标的名称或者资源不存在?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论