How do I replace embedded resources in a .NET assembly programmatically?

老子叫甜甜 提交于 2019-12-18 12:54:59

问题


I am trying to replace a Resource of an exe(.NET, C#) file using C# code.

I have found this article and made this code (using Mono.Cecil 0.6):

AssemblyDefinition asdDefinition = AssemblyFactory.GetAssembly("C:\\File.exe");
EmbeddedResource erTemp = new EmbeddedResource("encFile", ManifestResourceAttributes.Public);
erTemp.Data = myNewFileBytes;
asdDefinition.MainModule.Resources.RemoveAt(0);
asdDefinition.MainModule.Resources.Add(erTemp);
AssemblyFactory.SaveAssembly(asdDefinition, "C:\\newFile.exe");

The code is actually removing the resource and then adding a new one with the same name. The resource name is encFile and stored as encFile.exe (tried both).

I tested the code and the remove is working (i can tell by the size of the file) and the adding too, but the new file crash just like the file i created with the remove only (for the testing) - it acts like he can't see the replaced resource.

What can i do to fix it up? maybe some changes in the edited EXE file? the EXE file read its resource this way: byte[] buffer = ProjectName.Properties.Resources.encFile;


回答1:


try checking the below code project article it may be helpful

http://www.codeproject.com/KB/dotnet/embeddedresources.aspx



来源:https://stackoverflow.com/questions/6926378/how-do-i-replace-embedded-resources-in-a-net-assembly-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!