Updated to use modified date, shortened code a bit.

This commit is contained in:
Kevin Laskey 2024-06-13 08:35:13 -04:00
parent 11cb2e0efd
commit 4f7a762bf1

View File

@ -232,6 +232,12 @@ namespace VEPROMS
{ // C2022-029 if an existing export of the same name is found, provide option to overwrite it { // C2022-029 if an existing export of the same name is found, provide option to overwrite it
DialogResult ovewriteEx = FlexibleMessageBox.ShowCustom(null, "There is already another export file with the same name. You can choose to either overwrite the existing file or have the existing file renamed with the original creation date appended.\r\n\r\nSelecting 'Cancel' will cancel the export.", "What would you like to do?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);// == DialogResult.Yes; DialogResult ovewriteEx = FlexibleMessageBox.ShowCustom(null, "There is already another export file with the same name. You can choose to either overwrite the existing file or have the existing file renamed with the original creation date appended.\r\n\r\nSelecting 'Cancel' will cancel the export.", "What would you like to do?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);// == DialogResult.Yes;
// Extract directory, filename, and extension
string directory = Path.GetDirectoryName(fileLocation);
string filename = Path.GetFileNameWithoutExtension(fileLocation);
string extension = Path.GetExtension(fileLocation);
fileLocation = $"{directory}\\{filename}{extension}";
if (ovewriteEx == DialogResult.Abort) if (ovewriteEx == DialogResult.Abort)
{ {
MessageBox.Show("Export has been cancelled", "You have chosen to cancel the export.", MessageBoxButtons.OK, MessageBoxIcon.Information); // C2020-042 changed mesage box title MessageBox.Show("Export has been cancelled", "You have chosen to cancel the export.", MessageBoxButtons.OK, MessageBoxIcon.Information); // C2020-042 changed mesage box title
@ -240,33 +246,17 @@ namespace VEPROMS
} }
else if (ovewriteEx == DialogResult.Retry) else if (ovewriteEx == DialogResult.Retry)
{ {
// Ovewrite //Overwrite will occur, set msg.
// Extract directory, filename, and extension
string directory = Path.GetDirectoryName(fileLocation);
string filename = Path.GetFileNameWithoutExtension(fileLocation);
string extension = Path.GetExtension(fileLocation);
fileLocation = $"{directory}\\{filename}{extension}";
msg = "The export file has been overwritten. "; msg = "The export file has been overwritten. ";
} }
else if (ovewriteEx == DialogResult.Ignore) else if (ovewriteEx == DialogResult.Ignore)
{ {
// Extract directory, filename, and extension // Get the modified date of the existing file, create a datestamp for use in name, set newlocation to move to
string directory = Path.GetDirectoryName(fileLocation); DateTime modifiedDate = File.GetLastWriteTime(fileLocation);
string filename = Path.GetFileNameWithoutExtension(fileLocation); string datestamp = modifiedDate.ToString("yyyyMMddHHmmss");
string extension = Path.GetExtension(fileLocation);
// Generate the new filename with a datestamp
fileLocation = $"{directory}\\{filename}{extension}";
// Get the creation date of the existing file
DateTime creationDate = File.GetCreationTime(fileLocation);
// Format the creation date to use it as a datestamp
string datestamp = creationDate.ToString("yyyyMMddHHmmss");
string newFileLocation = $"{directory}\\{filename}_{datestamp}{extension}"; string newFileLocation = $"{directory}\\{filename}_{datestamp}{extension}";
//Move and set msg.
File.Move(fileLocation, newFileLocation); File.Move(fileLocation, newFileLocation);
msg = "The previous export has been renamed, the export file has been created. "; msg = "The previous export has been renamed, the export file has been created. ";
} }