【问题标题】:How to Extract the Resouce Content From MSIL OR .NET PE Files如何从 MSIL 或 .NET PE 文件中提取资源内容
【发布时间】:2015-05-21 14:02:02
【问题描述】:

请检查图片链接,我需要从 MSIL 文件中提取资源内容。我已经使用 ILSpy 调试了该文件,但我需要以任何其他方式进行调试。无需使用任何人工干预。

http://i.stack.imgur.com/ZQdRc.png

【问题讨论】:

  • 您实际上想要完成什么?你想用代码做到这一点吗?为什么 ILSpy 不够用?
  • 我需要提取更多大约 20 多个文件。我无法在 ILspy 中打开每个文件并手动提取。需要任何命令行类型。例如。 Extracter.exe

标签: .net portable-executable disassembly ilspy


【解决方案1】:

你可以这样做:

public class LoadAssemblyInfo : MarshalByRefObject
{
    public string AssemblyName { get; set; }

    public Tuple<string, byte[]>[] Streams;

    public void Load()
    {
        Assembly assembly = Assembly.ReflectionOnlyLoad(AssemblyName);

        string[] resources = assembly.GetManifestResourceNames();

        var streams = new List<Tuple<string, byte[]>>();

        foreach (string resource in resources)
        {
            ManifestResourceInfo info = assembly.GetManifestResourceInfo(resource);

            using (var stream = assembly.GetManifestResourceStream(resource))
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);

                streams.Add(Tuple.Create(resource, bytes));
            }
        }

        Streams = streams.ToArray();
    }
}

// Adapted from from http://stackoverflow.com/a/225355/613130
public static Tuple<string, byte[]>[] LoadAssembly(string assemblyName)
{
    LoadAssemblyInfo lai = new LoadAssemblyInfo
    {
        AssemblyName = assemblyName,
    };

    AppDomain tempDomain = null;

    try
    {
        tempDomain = AppDomain.CreateDomain("TemporaryAppDomain");
        tempDomain.DoCallBack(lai.Load);
    }
    finally
    {
        if (tempDomain != null)
        {
            AppDomain.Unload(tempDomain);
        }
    }

    return lai.Streams;
}

像这样使用它:

var streams = LoadAssembly("EntityFramework");

streamsTuple&lt;string, byte[]&gt; 的数组,其中Item1 是资源的名称,Item2 是资源的二进制内容。

它相当复杂,因为它在另一个 AppDomain 中执行 Assembly.ReflectionOnlyLoad,然后卸载 (AppDomain.CreateDomain/AppDomain.Unload)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多