C# ExtractAssemblyResources

Date: 2023-11-17
using System.Collections;
using System.Reflection;
using System.Resources;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.WriteLine("Geef de naam van de assembly als argument op.");
            return;
        }

        try
        {
            ExtractAssemblyResources(args[0]);
            Console.WriteLine("Alle .png resources zijn succesvol opgeslagen.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Er is een fout opgetreden: {ex.Message}");
        }
    }

    private static void ExtractAssemblyResources(string assemblyName)
    {
        var assembly = Assembly.LoadFrom(assemblyName);
        if (assembly == null) return;

        var resources = assembly.GetManifestResourceNames();
        Console.WriteLine($"Resource count: {resources.Count()}");

        var outputDir = Path.Combine(Path.GetDirectoryName(assemblyName), "ExtractedImages");
        Directory.CreateDirectory(outputDir);

        foreach (var resourceName in resources)
        {
            Console.WriteLine($"Resource: {resourceName}");
            try
            {
                if (resourceName.EndsWith(".resources"))
                {
                    ExtractNestedResource(assembly, resourceName, outputDir);
                }
                else if (resourceName.EndsWith(".png"))
                {
                    ExtractResource(assembly, resourceName, outputDir);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error extracting resource '{resourceName}': {ex}");
            }
        }
    }

    static void ExtractResource(Assembly assembly, string resourceName, string outputDir)
    {
        string outputFilePath = Path.Combine(outputDir, resourceName.Replace("/", "_"));
        using var resourceStream = assembly.GetManifestResourceStream(resourceName);
        using var outputStream = new FileStream(outputFilePath, FileMode.Create);
        resourceStream.CopyTo(outputStream);
        Console.WriteLine($"Resource '{resourceName}' opgeslagen als '{outputFilePath}'.");
    }

    static void ExtractNestedResource(Assembly assembly, string resourceName, string outputDir)
    {
        using var resourceStream = assembly.GetManifestResourceStream(resourceName);
        using var resourceReader = new ResourceReader(resourceStream);
        foreach (DictionaryEntry resource in resourceReader)
        {
            string childName = $"{resource.Key}";
            string outputFilePath = Path.Combine(outputDir, childName.Replace("/", "_"));
            Console.WriteLine($"Nested resource: {resourceName}/{resource.Key}");
            if (!childName.EndsWith(".png")) continue;

            try
            {
                using var outputStream = new FileStream(outputFilePath, FileMode.Create);
                if (resource.Value is Stream childResourceStream)
                {
                    childResourceStream.CopyTo(outputStream);
                }
                else if (resource.Value is byte[] buffer)
                {
                    outputStream.Write(buffer, 0, buffer.Length);
                }
                Console.WriteLine($"Geneste resource '{resource.Key}' uit '{resourceName}' opgeslagen als '{outputFilePath}'.");
            }
            catch (Exception ex) 
            {
                Console.WriteLine($"Error extracting nested resource '{resource.Key}' from '{resourceName}': {ex}");
            }
        }
    }
}
81380cookie-checkC# ExtractAssemblyResources