Reputation: 201
I have a strange behavior on Windows 10 Pro. I have written the following C# code, which seems to work for 2 seconds until the file (txt/pdf) changes back TO ITS ORIGINAL modification / creation time. Maybe someone can explain that to me?
static void Main(string[] args)
{
var filePath = args[0];
var dateTimeStr = args[1];
try
{
DateTime dateTime;
var isOK = DateTime.TryParse(dateTimeStr,out dateTime);
if (!isOK)
{
Console.Write(string.Format("Could not parse date <{0}>! Try format <'2019-03-12 12:14:01 AM'> Exiting...", dateTimeStr));
return;
}
var fileInfo = new FileInfo(filePath);
if (!fileInfo.Exists)
{
Console.Write(string.Format("File <{0}> does not exist! Exiting...",fileInfo.FullName));
return;
}
Console.Write("Will set date <{0}> on file <{1}>. Continue [yY]?", fileInfo.FullName, dateTimeStr);
var confirm = Console.ReadLine();
if (confirm.ToLower().Contains("y"))
{
// This does not work on the original file
File.SetLastAccessTime(fileInfo.FullName, dateTime);
File.SetCreationTime(fileInfo.FullName, dateTime);
File.SetLastWriteTime(fileInfo.FullName, dateTime);
Console.WriteLine("Changed date to <{0}> on file <{1}>.", fileInfo.FullName, File.GetLastWriteTime(fileInfo.FullName));
}
else
{
Console.WriteLine("Aborted!");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
Upvotes: 2
Views: 185
Reputation: 155726
(Converting my comment to an answer:)
Use ProcMon, part of Microsoft Sysinternals, to monitor filesystem activity (and many other kinds of program activity) to see exactly why the file dates are being reset.
I'll bet your program is probably just fighting with poorly-written file/folder synchronization software, like OneDrive or DropBox or dodgy anti-virus software.
Upvotes: 2