add fixedfilemove

This commit is contained in:
Bitl 2021-09-13 08:41:10 -07:00
parent 37647ebbce
commit 72ae016692
2 changed files with 45 additions and 9 deletions

View File

@ -46,11 +46,9 @@ public static class TextLineRemover
}
}
}
// Delete original file
GlobalFuncs.FixedFileDelete(filename);
// ... and put the temp file in its place.
File.Move(tempFilename, filename);
//FixedFileMove deletes the original file and moves the temp file in.
GlobalFuncs.FixedFileMove(tempFilename, filename, true);
// Final calculations
DateTime endTime = DateTime.Now;

View File

@ -593,10 +593,7 @@ public class GlobalFuncs
{
if (overwrite && overwritewarning)
{
DialogResult box = MessageBox.Show("A file with a similar name was detected in the directory as '" + dest +
"'.\n\nWould you like to override it?", "Novetus - Override Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (box == DialogResult.No)
if (ShowOverrideWarning(dest) == DialogResult.No)
{
return;
}
@ -618,6 +615,44 @@ public class GlobalFuncs
}
}
public static void FixedFileMove(string src, string dest, bool overwrite, bool overwritewarning = false)
{
if (!File.Exists(dest))
{
File.SetAttributes(src, FileAttributes.Normal);
File.Move(src, dest);
}
else
{
if (overwrite)
{
if (overwritewarning)
{
if (ShowOverrideWarning(dest) == DialogResult.No)
{
return;
}
}
FixedFileDelete(dest);
File.SetAttributes(src, FileAttributes.Normal);
File.Move(src, dest);
}
else
{
throw new IOException("Cannot create a file when that file already exists. FixedFileMove cannot override files with overwrite disabled.");
}
}
}
private static DialogResult ShowOverrideWarning(string dest)
{
DialogResult box = MessageBox.Show("A file with a similar name was detected in the directory as '" + dest +
"'.\n\nWould you like to override it?", "Novetus - Override Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
return box;
}
//modified from the following:
//https://stackoverflow.com/questions/28887314/performance-of-image-loading
//https://stackoverflow.com/questions/2479771/c-why-am-i-getting-the-process-cannot-access-the-file-because-it-is-being-u
@ -1850,6 +1885,8 @@ public class GlobalFuncs
return foundFiles.ToArray();
}
//https://stackoverflow.com/questions/66667263/i-want-to-remove-special-characters-from-file-name-without-affecting-extension-i
//https://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp
public static void RenameFileWithInvalidChars(string path)
@ -1857,8 +1894,9 @@ public class GlobalFuncs
string pathWithoutFilename = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
fileName = Regex.Replace(fileName, @"[^\w-.'_! ]", "");
string finalPath = pathWithoutFilename + "\\" + fileName;
File.Move(path, pathWithoutFilename + "\\" + fileName);
FixedFileMove(path, finalPath, true);
}
#if LAUNCHER || CMD || URI