如何保存组装盘? [英] How to save assembly to disk?

查看:147
本文介绍了如何保存组装盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何保存组件文件?即我的意思是不是动态的组装,而是正常的内存组件。

How I can save assembly to file? I.e. I mean not dynamic assembly but "normal" in-memory assemblies.

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

当应用程序从资源加载一些引用的程序会出现这种情况。我想将它们保存到磁盘。

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

有不可能提取组件形成资源,因为它们是有加密

It is impossible to extract assemblies form resources because they are encrypted there.

推荐答案

。 请注意,我试图坚持命名约定。

From the idea of Greg Ros i developed this little snippet. Please note that i tried to stick to the 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();
            }
        }
    }
}   

但有些组件没有标记为可序列化,例如mscorlib.dll中。 因此,这可能只是部分解决?

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

尽管这是可能的序列化的部分的组件,我建议使用 FileInfo的作为例子提供,生成一个列表,并检查 组件。

Despite that it is possible to serialize some assemblies, I suggest using the FileInfo as provided in the example, generate a list and inspect the original assemblies.

这篇关于如何保存组装盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆