// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Spludlow { public class TempDirectory : IDisposable { private string LockFilePath; public string Path; public TempDirectory() { this.Start(null); } public TempDirectory(string rootDir) { this.Start(rootDir); } private void Start(string rootDir) { if (rootDir != null && rootDir.StartsWith("@") == true) { string configKey = rootDir.Substring(1); rootDir = Spludlow.Config.Get(configKey, true); if (rootDir == null) Spludlow.Log.Warning("TempDirectory; Config Key not found, Using default Temp Directory Location:\t" + configKey); } if (rootDir == null || rootDir == "") { if (rootDir == null) this.LockFilePath = System.IO.Path.GetTempFileName(); else this.LockFilePath = @"\\?\" + System.IO.Path.GetTempFileName(); // Long filename support this.Path = this.LockFilePath + ".dir"; } else { this.LockFilePath = null; this.Path = Spludlow.Io.Dirs.UniqueExistingName(rootDir + @"\Temp-" + Spludlow.Text.TimeStamp()); } Directory.CreateDirectory(this.Path); } public void Dispose() { if (Directory.Exists(this.Path) == true) { Directory.Delete(this.Path, true); } if (this.LockFilePath != null) File.Delete(this.LockFilePath); } public override string ToString() { return this.Path; } } }