-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTimestomp.cs
62 lines (50 loc) · 1.65 KB
/
Timestomp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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");