如何以编程方式替换 .NET 程序集中的嵌入资源?

发布于 2024-11-27 13:47:55 字数 940 浏览 5 评论 0原文

我正在尝试使用 C# 代码替换 exe(.NET、C#)文件的资源。

我发现 这篇文章并编写了这段代码(使用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");

该代码实际上是删除资源,然后添加一个同名的新资源。 资源名称为 encFile 并存储为 encFile.exe(两者都尝试过)。

我测试了代码,删除正在工作(我可以通过文件的大小来判断)并且添加也有效,但是新文件崩溃就像我仅使用删除创建的文件(用于测试) - 它的行为就像他看不到替换的资源。

我可以做什么来修复它?也许编辑后的 ​​EXE 文件发生了一些变化? EXE 文件通过以下方式读取其资源:

byte[] buffer = ProjectName.Properties.Resources.encFile;

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? Maybe some changes in the edited EXE file?
The EXE file reads its resource this way:

byte[] buffer = ProjectName.Properties.Resources.encFile;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2024-12-04 13:47:55

尝试这样做似乎过于复杂。如果您需要动态更新资源,请将资源作为应用程序的文件夹发送(将文件夹中的项目设置为内容,如果项目属性中较新,则进行复制)。

如果您需要在运行时动态更新,则很简单:

1] 允许用户就地替换项目或
2] 更好的是,将其视为 word-press 主题,并允许每个资源有一个覆盖文件夹。

如果您需要使用元数据标记每个资源,您可以使用 sqlite 数据库,甚至更简单,允许每个资源使用匹配的 .meta 文件来更详细地描述它。

最后,如果您允许软件的数字下载,那么您可能会考虑对可执行文件进行代码签名 - 在这种情况下,以任何方式修改可执行文件都不是一个选择。

Trying to do this seems overly complex. If you need dynamic update of resources, ship your resources as a folder for your application (set items in the folder as content and copy if newer in project properties).

If you need dynamic update at runtime, then it's as simple as either:

1] Allow user to replace items in place or
2] Even better, treat it like word-press themes and allow an override folder for each resource.

If you need to tag each resource with metadata you could use a sqlite database or even easier, allow a matching .meta file for each resource to describe it in more detail.

Finally, if you are allowing digital download of your software, then you might consider code-signing your executable - in which case modifying the executable in any way will not be an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文