【问题标题】:Embed a file into an external exe with C#使用 C# 将文件嵌入到外部 exe 中
【发布时间】:2017-06-14 20:22:59
【问题描述】:

所以我制作了一种解释型编程语言,一切都很好,除了一件事:我想制作一个“编译器”,它将用户的代码嵌入到解释器的副本中,这样所有用户都需要只需双击嵌入代码的可执行文件,它就会运行。

所以问题是:如何将文件嵌入到已编译的可执行文件中?我确实可以访问可执行文件之前它被编译,但嵌入过程必须发生在之后。

我更喜欢 C# 中的解决方案,但我很绝望,可以使用 C++ 或 VB.NET,甚至是批处理文件

【问题讨论】:

  • 您要嵌入哪种类型的文件?
  • @FeliceM 只是一个包含将被解释的代码的文本文件

标签: c# file embed executable interpreter


【解决方案1】:

所以经过一番激烈的谷歌搜索后,我找到了一个名为 Mono.Cecil 的库,它完全符合我的要求。我可以使用以下代码将文件注入可执行文件:

string fileName = "Interpreter.exe"; // The file which will have toInject injected in it
string outputName = "Compiled.exe";  // The output file to compile
string toInject = "program.txt";     // The file we will be injecting into fileName
string resourceName = "program.txt"; // The name of the file once it's inside fileName

var module = ModuleDefinition.ReadModule(filename);

var file = new EmbeddedResource(
    resourceName,
    ManifestResourceAttributes.Private,
    File.ReadAllBytes(toInject));

module.Resources.Add(file);

module.Write(outputName);

【讨论】:

    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 2020-10-05
    • 2017-09-04
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多