/* // Exepack.NET temporary file manager // http://www.codeplex.com/exepack // Written by Y [06-03-09] Copyright (c) 2008-2009 Alexey Yakovlev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Exepack { /// /// Manages temporary directory and files, /// either inside or outside temporary directory /// class TempFileManager : IDisposable { public TempFileManager() { CreateTemporaryDirectory(); Files = new List(); } public TempFileManager(bool forceCleanup) : this() { ForceCleanup = forceCleanup; } ~TempFileManager() { Dispose(); } /// /// Deletes registered temporary files and directory /// public void Dispose() { // check if already disposed if (DirectoryName == null) { return; } try { // delete registered temporary files foreach (string fileName in Files) { File.Delete(this[fileName]); } } catch { // by default, clean up ignores errors if (ThrowExceptionsOnCleanup) throw; } finally { Files.Clear(); } try { // clean up temporary directory Directory.Delete(DirectoryName, ForceCleanup); } catch { // by default, clean up ignores all exceptions if (ThrowExceptionsOnCleanup) throw; } finally { DirectoryName = null; } } /// /// Creates directory with random name /// private void CreateTemporaryDirectory() { DirectoryName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(DirectoryName); } /// /// Registers file as temporary and returns full name inside the temporary directory /// public string this[string fileName] { get { // register file for cleanup if (!Files.Contains(fileName)) { Files.Add(fileName); } // if full path is specified, don't add temporary directory name if (Path.IsPathRooted(fileName)) { return fileName; } // put the file inside temporary directory return Path.Combine(DirectoryName, fileName); } } /// /// Directory to hold temporary files /// Will be cleaned up automatically /// public string DirectoryName { get; private set; } /// /// Temporary file list /// Files are not necessarily located in temporary directory /// public List Files { get; private set; } /// /// Set to true if temporary directory should be deleted even if not empty /// public bool ForceCleanup { get; set; } /// /// Set to true if you want to handle exceptions on cleanup yourself /// public bool ThrowExceptionsOnCleanup { get; set; } } }