// Copyright (C) Stichting Deltares 2021. All rights reserved. // // This file is part of Riskeer. // // Riskeer is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.IO; using System.Reflection; using Core.Common.Util.Reflection; namespace Core.Common.Util.IO { /// /// Class for writing Embedded Resources to the Windows Temp directory. /// public class EmbeddedResourceFileWriter : IDisposable { private readonly bool removeFilesOnDispose; /// /// Initializes a new instance of the class. /// /// The assembly that embeds the resources to write. /// Whether or not the files should be removed after /// disposing the created instance. /// The names of the Embedded Resource files to /// (temporarily) write to the Windows Temp directory. /// Thrown when an embedded resource file in /// cannot be found in . /// Thrown when /// or is null. public EmbeddedResourceFileWriter(Assembly assembly, bool removeFilesOnDispose, params string[] embeddedResourceFileNames) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } if (embeddedResourceFileNames == null) { throw new ArgumentNullException(nameof(embeddedResourceFileNames)); } this.removeFilesOnDispose = removeFilesOnDispose; TargetFolderPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(TargetFolderPath); foreach (string embeddedResourceFileName in embeddedResourceFileNames) { WriteEmbeddedResourceToTemporaryFile(assembly, embeddedResourceFileName, Path.Combine(TargetFolderPath, embeddedResourceFileName)); } } /// /// Gets the (automatically generated) target folder path. /// public string TargetFolderPath { get; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (removeFilesOnDispose && disposing) { DirectoryHelper.TryDelete(TargetFolderPath); } } private static void WriteEmbeddedResourceToTemporaryFile(Assembly assembly, string embeddedResourceFileName, string filePath) { Stream stream = GetStreamToFileInResource(assembly, embeddedResourceFileName); byte[] bytes = GetBinaryDataOfStream(stream); File.WriteAllBytes(filePath, bytes); } private static Stream GetStreamToFileInResource(Assembly assembly, string embeddedResourceFileName) { return AssemblyUtils.GetAssemblyResourceStream(assembly, embeddedResourceFileName); } private static byte[] GetBinaryDataOfStream(Stream stream) { var bytes = new byte[stream.Length]; var reader = new BinaryReader(stream); reader.Read(bytes, 0, (int) stream.Length); return bytes; } } }