How to save assembly to disk?

How can I save the assembly to a file? That is, I do not mean dynamic assembly, but "normal" assemblies in memory.

Assembly[] asslist = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly ass1 in asslist) { // How to save? } 

This situation may occur when an application loads some referenced assemblies from resources. I want to save them to disk.

It is not possible to extract assembly resources because they are encrypted there.

+4
source share
3 answers

How about trying to serialize the assembly? It is serializable .

0
source

You need to find the path from which your ass[...]es came. You can find it as follows:

 Assembly ass = ...; return ass.Location; 

Please note that as is a keyword and cannot be used as an identifier. I recommend using ass .

0
source

From the idea of ​​Greg Roses, I developed this little fragment. Please note that I tried to adhere to naming conventions.

 public void SaveAllAssemblies() { Assembly[] asslist = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly ass in asslist) { FileInfo fi = new FileInfo(ass.Location); if (!fi.Extension.Equals(".exe", StringComparison.InvariantCultureIgnoreCase)) { var assName = fi.Name; var assConverter = new FormatterConverter(); var assInfo = new SerializationInfo(typeof(Assembly), assConverter); var assContext = new StreamingContext(); using (var assStream = new FileStream(assName, FileMode.Create)) { BinaryFormatter bformatter = new BinaryFormatter(); ass.GetObjectData(assInfo, assContext); bformatter.Serialize(assStream, assInfo); assStream.Close(); } } } } 

But some assemblies are not marked as serializable, such as mscorlib.dll. Therefore, is this probably only a partial solution?

Despite the fact that you can serialize some assemblies, I suggest using FileInfo, as shown in the example, will generate a list and check the original nodes.

0
source

All Articles