diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index ead6efca..8a5e0ef8 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -3649,9 +3649,9 @@ namespace SabreTools.Library.DatFiles string newBasePath = basePath; if (copyFiles) { - newBasePath = Path.Combine(tempDir, new Guid().ToString()); + newBasePath = Path.Combine(tempDir, Guid.NewGuid().ToString()); newItem = Path.GetFullPath(Path.Combine(newBasePath, Path.GetFullPath(item).Remove(0, basePath.Length + 1))); - Directory.CreateDirectory(Path.GetDirectoryName(newItem)); + Utilities.TryCreateDirectory(Path.GetDirectoryName(newItem)); File.Copy(item, newItem, true); } diff --git a/SabreTools.Library/FileTypes/SevenZipArchive.cs b/SabreTools.Library/FileTypes/SevenZipArchive.cs index 542d6948..46622615 100644 --- a/SabreTools.Library/FileTypes/SevenZipArchive.cs +++ b/SabreTools.Library/FileTypes/SevenZipArchive.cs @@ -395,7 +395,7 @@ namespace SabreTools.Library.FileTypes }; // Create the temp directory - string tempPath = Path.Combine(outDir, new Guid().ToString()); + string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString()); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); @@ -608,7 +608,7 @@ namespace SabreTools.Library.FileTypes keys.Sort(ZipFile.TorrentZipStringCompare); // Create the temp directory - string tempPath = Path.Combine(outDir, new Guid().ToString()); + string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString()); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); diff --git a/SabreTools.Library/FileTypes/XZArchive.cs b/SabreTools.Library/FileTypes/XZArchive.cs index 8ddd00b6..5e73c16b 100644 --- a/SabreTools.Library/FileTypes/XZArchive.cs +++ b/SabreTools.Library/FileTypes/XZArchive.cs @@ -289,7 +289,7 @@ namespace SabreTools.Library.FileTypes }; // Create the temp directory - string tempPath = Path.Combine(outDir, new Guid().ToString()); + string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString()); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); @@ -502,7 +502,7 @@ namespace SabreTools.Library.FileTypes keys.Sort(ZipFile.TorrentZipStringCompare); // Create the temp directory - string tempPath = Path.Combine(outDir, new Guid().ToString()); + string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString()); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); diff --git a/SabreTools.Library/Tools/Utilities.cs b/SabreTools.Library/Tools/Utilities.cs index 6a404f5d..8282a642 100644 --- a/SabreTools.Library/Tools/Utilities.cs +++ b/SabreTools.Library/Tools/Utilities.cs @@ -1775,6 +1775,33 @@ namespace SabreTools.Library.Tools } } + /// + /// Try to safely delete a directory, optionally throwing the error + /// + /// Name of the directory to delete + /// True if the error that is thrown should be thrown back to the caller, false otherwise + /// True if the file didn't exist or could be deleted, false otherwise + public static bool TryCreateDirectory(string file, bool throwOnError = false) + { + // Now wrap creating the directory + try + { + Directory.CreateDirectory(file); + return true; + } + catch (Exception ex) + { + if (throwOnError) + { + throw ex; + } + else + { + return false; + } + } + } + /// /// Try to safely delete a directory, optionally throwing the error /// @@ -1783,13 +1810,13 @@ namespace SabreTools.Library.Tools /// True if the file didn't exist or could be deleted, false otherwise public static bool TryDeleteDirectory(string file, bool throwOnError = false) { - // Check if the file exists first + // Check if the directory exists first if (!Directory.Exists(file)) { return true; } - // Now wrap deleting the file + // Now wrap deleting the directory try { Directory.Delete(file, true);