Laden...

Resource Extraction Tool

Erstellt von egrath vor 18 Jahren Letzter Beitrag vor 18 Jahren 3.700 Views
egrath Themenstarter:in
871 Beiträge seit 2005
vor 18 Jahren
Resource Extraction Tool

Hallo,

da ich heute ein Tool gebraucht habe um Resourcen aus einer Assembly zu extrahieren hab ich kurz was zusammengehackt welches dies bewerkstelligt. Ist zwar kein wirkliches Projekt, aber vielleicht kann jemand das sonst noch brauchen.

(Achtung: quick'n'dirty Style, also nix besonderes erwarten 😉


using System;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Resources;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace ResReader
{
    class Program
    {
        static void Main( string[] args )
        {
            if( args.Length <= 0 )
            {
                Console.Out.WriteLine( "illegal params" );
                System.Environment.Exit( -1 );
            }

            Assembly resAssembly = null;
            try
            {
                resAssembly = Assembly.LoadFrom( args[0] );
            }
            catch( Exception e )
            {
                Console.Out.WriteLine( e.ToString() );
                System.Environment.Exit( -1 );
            }

            Stream resStream = null;
            foreach( string resName in resAssembly.GetManifestResourceNames() )
            {
                Console.Out.WriteLine( "Resource: {0}", resName );
                resStream = resAssembly.GetManifestResourceStream( resName );
                Console.Out.WriteLine( "   Length: {0}", resStream.Length );

                
                ResourceReader reader;
                try
                {
                    reader = new ResourceReader( resStream );
                }
                catch( ArgumentException )
                {
                    Console.Out.WriteLine( "Exception catched: Stream is not a valid Resource" );
                    reader = null;
                }

                if( reader != null )
                {
                    IDictionaryEnumerator id = reader.GetEnumerator();
                    while( id.MoveNext() )
                    {
                        string resourceType;
                        byte[] resourceData = new byte[resStream.Length];
                        reader.GetResourceData( id.Key.ToString(), out resourceType, out resourceData );
                        Console.Out.Write( "      {0} : {1}; Len = {2} ", id.Key, id.Value, resourceData.Length );

                        Stream resourceStream = new MemoryStream( resourceData );
                        BinaryFormatter formatter = new BinaryFormatter();
                        
                        string outFileName = String.Empty;
                        if( id.Value != null && id.Value.ToString() == "System.Drawing.Bitmap" )
                        { 
                            // Image Resources are saved to BMP
                            outFileName = String.Format( "{0}.{1}", id.Key, "bmp" );
                            Console.Out.WriteLine( "=> {0}", outFileName );

                            Bitmap outBitmap = ( Bitmap ) formatter.Deserialize( resourceStream );
                            outBitmap.Save( outFileName, System.Drawing.Imaging.ImageFormat.Bmp );
                        }
                        else
                        {
                            // Other Resources are currently not handled
                            outFileName = id.Key.ToString() + ".unknown";
                            Console.Out.WriteLine( "=> {0} (not saved)", outFileName );
                        }                    
                                        
                    }
                }
            }
        }
    }
}

Grüsse, Egon

/edit: Noch ne kleine Änderung gemacht