Last active
August 9, 2022 14:32
-
-
Save DarkCoderSc/36c4912e92c783816c64bd51259cc752 to your computer and use it in GitHub Desktop.
https://unprotect.it/snippet/timestomp/121/ - Author: Jean-Pierre LESUEUR (@DarkCoderSc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
void timeStomp(String targetFile) | |
{ | |
targetFile = Path.GetFullPath(targetFile); | |
if (!File.Exists(targetFile)) | |
{ | |
throw new FileNotFoundException(String.Format("File \"{0}\" does not exists.", targetFile)); | |
} | |
string? parentDirectory = Path.GetDirectoryName(targetFile); | |
bool isInRoot = false; | |
if (parentDirectory == null) | |
{ | |
parentDirectory = Directory.GetDirectoryRoot(targetFile); | |
isInRoot = true; | |
} | |
var options = new EnumerationOptions() | |
{ | |
IgnoreInaccessible = true, | |
RecurseSubdirectories = true, | |
AttributesToSkip = FileAttributes.System | FileAttributes.Hidden, | |
}; | |
var candidates = new DirectoryInfo(parentDirectory) | |
.GetFiles("*.*", options) | |
.Where(file => !file.FullName.Equals(targetFile, StringComparison.OrdinalIgnoreCase)) | |
.OrderByDescending(file => file.LastWriteTime) | |
.ToList(); | |
FileInfo? candidate = null; | |
if (candidates.Count > 0) | |
{ | |
candidate = candidates.First(); | |
} | |
else if (!isInRoot) | |
{ | |
candidate = new FileInfo(parentDirectory); | |
} | |
if (candidate != null) | |
{ | |
Console.WriteLine(string.Format("Using \"{0}\" file for timeStomping...", candidate)); | |
File.SetCreationTime(targetFile, candidate.CreationTime); | |
File.SetLastAccessTime(targetFile, candidate.LastAccessTime); | |
File.SetLastWriteTime(targetFile, candidate.LastWriteTime); | |
Console.WriteLine("Done."); | |
} | |
else | |
{ | |
throw new Exception("Could not find suitable existing file for timeStomping..."); | |
} | |
} | |
timeStomp("G:\\test\\sub7.exe"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment