{"id":8138,"date":"2023-11-17T09:16:11","date_gmt":"2023-11-17T08:16:11","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8138"},"modified":"2023-11-17T09:16:13","modified_gmt":"2023-11-17T08:16:13","slug":"c-extractassemblyresources","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-extractassemblyresources\/","title":{"rendered":"C# ExtractAssemblyResources"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using System.Collections;\nusing System.Reflection;\nusing System.Resources;\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        if (args.Length != 1)\n        {\n            Console.WriteLine(\"Geef de naam van de assembly als argument op.\");\n            return;\n        }\n\n        try\n        {\n            ExtractAssemblyResources(args[0]);\n            Console.WriteLine(\"Alle .png resources zijn succesvol opgeslagen.\");\n        }\n        catch (Exception ex)\n        {\n            Console.WriteLine($\"Er is een fout opgetreden: {ex.Message}\");\n        }\n    }\n\n    private static void ExtractAssemblyResources(string assemblyName)\n    {\n        var assembly = Assembly.LoadFrom(assemblyName);\n        if (assembly == null) return;\n\n        var resources = assembly.GetManifestResourceNames();\n        Console.WriteLine($\"Resource count: {resources.Count()}\");\n\n        var outputDir = Path.Combine(Path.GetDirectoryName(assemblyName), \"ExtractedImages\");\n        Directory.CreateDirectory(outputDir);\n\n        foreach (var resourceName in resources)\n        {\n            Console.WriteLine($\"Resource: {resourceName}\");\n            try\n            {\n                if (resourceName.EndsWith(\".resources\"))\n                {\n                    ExtractNestedResource(assembly, resourceName, outputDir);\n                }\n                else if (resourceName.EndsWith(\".png\"))\n                {\n                    ExtractResource(assembly, resourceName, outputDir);\n                }\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine($\"Error extracting resource '{resourceName}': {ex}\");\n            }\n        }\n    }\n\n    static void ExtractResource(Assembly assembly, string resourceName, string outputDir)\n    {\n        string outputFilePath = Path.Combine(outputDir, resourceName.Replace(\"\/\", \"_\"));\n        using var resourceStream = assembly.GetManifestResourceStream(resourceName);\n        using var outputStream = new FileStream(outputFilePath, FileMode.Create);\n        resourceStream.CopyTo(outputStream);\n        Console.WriteLine($\"Resource '{resourceName}' opgeslagen als '{outputFilePath}'.\");\n    }\n\n    static void ExtractNestedResource(Assembly assembly, string resourceName, string outputDir)\n    {\n        using var resourceStream = assembly.GetManifestResourceStream(resourceName);\n        using var resourceReader = new ResourceReader(resourceStream);\n        foreach (DictionaryEntry resource in resourceReader)\n        {\n            string childName = $\"{resource.Key}\";\n            string outputFilePath = Path.Combine(outputDir, childName.Replace(\"\/\", \"_\"));\n            Console.WriteLine($\"Nested resource: {resourceName}\/{resource.Key}\");\n            if (!childName.EndsWith(\".png\")) continue;\n\n            try\n            {\n                using var outputStream = new FileStream(outputFilePath, FileMode.Create);\n                if (resource.Value is Stream childResourceStream)\n                {\n                    childResourceStream.CopyTo(outputStream);\n                }\n                else if (resource.Value is byte[] buffer)\n                {\n                    outputStream.Write(buffer, 0, buffer.Length);\n                }\n                Console.WriteLine($\"Geneste resource '{resource.Key}' uit '{resourceName}' opgeslagen als '{outputFilePath}'.\");\n            }\n            catch (Exception ex) \n            {\n                Console.WriteLine($\"Error extracting nested resource '{resource.Key}' from '{resourceName}': {ex}\");\n            }\n        }\n    }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8138","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8138"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8138\/revisions"}],"predecessor-version":[{"id":8139,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8138\/revisions\/8139"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}