Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

c# - Changing external exe icon at runtime

I'm developing a simple application which is supposed to simply copy another existing executable file and change it's icon. Is this possible to achieve with .NET? I'm asking about icon changing, copying is not a problem.

I have no clues on how and where to start. I've searched on Google, but found only unmanaged C++ code which I'm not very much familiar with.

This post has nothing with changing current application's icon at runtime, rather it's about changing the icon of an external application (that's visible in Windows Explorer).

Any help would be appreciated.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.NET has no support for this whatsoever. The icon is stored in the unmanaged resources embedded in a .exe. You can see them with File + Open + File, select the .exe and open the Icons node. The lowest numbered icon is the one Windows uses.

Updating those resources normally requires rebuilding the .exe. Luckily the Windows API has support for updating resources on-the-fly. It has restrictions but that shouldn't affect you for icons. You'll need BeginUpdateResource, UpdateResource and EndUpdateResource. Beware that they are not easy to use, you need to know the icon resource number. Visit pinvoke.net for the declarations you will need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...