如何保存组装盘?

2023-09-05 00:49:52 作者:@梁愛人

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

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.

相关推荐
 
精彩推荐
图片推荐