How do I replace embedded resources in a .NET asse

2019-02-06 02:29发布

问题:

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