[DatFile, FileTypes/, Utilities] GUID, but correct

This commit is contained in:
Matt Nadareski
2018-02-22 16:21:11 -08:00
parent 07f8dba600
commit b01ba56d6a
4 changed files with 35 additions and 8 deletions

View File

@@ -3649,9 +3649,9 @@ namespace SabreTools.Library.DatFiles
string newBasePath = basePath; string newBasePath = basePath;
if (copyFiles) 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))); 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); File.Copy(item, newItem, true);
} }

View File

@@ -395,7 +395,7 @@ namespace SabreTools.Library.FileTypes
}; };
// Create the temp directory // Create the temp directory
string tempPath = Path.Combine(outDir, new Guid().ToString()); string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString());
if (!Directory.Exists(tempPath)) if (!Directory.Exists(tempPath))
{ {
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
@@ -608,7 +608,7 @@ namespace SabreTools.Library.FileTypes
keys.Sort(ZipFile.TorrentZipStringCompare); keys.Sort(ZipFile.TorrentZipStringCompare);
// Create the temp directory // Create the temp directory
string tempPath = Path.Combine(outDir, new Guid().ToString()); string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString());
if (!Directory.Exists(tempPath)) if (!Directory.Exists(tempPath))
{ {
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);

View File

@@ -289,7 +289,7 @@ namespace SabreTools.Library.FileTypes
}; };
// Create the temp directory // Create the temp directory
string tempPath = Path.Combine(outDir, new Guid().ToString()); string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString());
if (!Directory.Exists(tempPath)) if (!Directory.Exists(tempPath))
{ {
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
@@ -502,7 +502,7 @@ namespace SabreTools.Library.FileTypes
keys.Sort(ZipFile.TorrentZipStringCompare); keys.Sort(ZipFile.TorrentZipStringCompare);
// Create the temp directory // Create the temp directory
string tempPath = Path.Combine(outDir, new Guid().ToString()); string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString());
if (!Directory.Exists(tempPath)) if (!Directory.Exists(tempPath))
{ {
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);

View File

@@ -1775,6 +1775,33 @@ namespace SabreTools.Library.Tools
} }
} }
/// <summary>
/// Try to safely delete a directory, optionally throwing the error
/// </summary>
/// <param name="file">Name of the directory to delete</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the file didn't exist or could be deleted, false otherwise</returns>
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;
}
}
}
/// <summary> /// <summary>
/// Try to safely delete a directory, optionally throwing the error /// Try to safely delete a directory, optionally throwing the error
/// </summary> /// </summary>
@@ -1783,13 +1810,13 @@ namespace SabreTools.Library.Tools
/// <returns>True if the file didn't exist or could be deleted, false otherwise</returns> /// <returns>True if the file didn't exist or could be deleted, false otherwise</returns>
public static bool TryDeleteDirectory(string file, bool throwOnError = false) 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)) if (!Directory.Exists(file))
{ {
return true; return true;
} }
// Now wrap deleting the file // Now wrap deleting the directory
try try
{ {
Directory.Delete(file, true); Directory.Delete(file, true);