Search code examples
.netportable-executabledisassemblyilspy

How to Extract the Resouce Content From MSIL OR .NET PE Files


Please check the Image link, i need to extract the resource content from the MSIL file. I have debugged the file using ILSpy but i need to do it in any other way. with out using any manual intereption.

https://i.sstatic.net/ZQdRc.png


Solution

  • You can do it with something like:

    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;
    }
    

    Use it like:

    var streams = LoadAssembly("EntityFramework");
    

    streams is an array of Tuple<string, byte[]>, where Item1 is the name of the resource and Item2 is the binary content of the resource.

    It is quite complex because it does the Assembly.ReflectionOnlyLoad in another AppDomain that is then unloaded (AppDomain.CreateDomain/AppDomain.Unload).