question

azeemukain-3195 avatar image
1 Vote"
azeemukain-3195 asked AgaveJoe commented

Cannot set creation time when a file is on a removable disk

I try to set Creation, Write and Access time for all files in a directory. And that works if file is on local machine. But when I try to do it for directory on a removable disk, I get an exception:

System.IO.IOException: 'The parameter is incorrect. : 'path'

I really don't know why it happens, but:

the path is correct
BitLocker is turned off
files don't have a read-only attribute

 static void Main(string[] args)
 {
     var files = new DirectoryInfo(args[0]).GetFiles();
    
     foreach (var file in files)
     {
         File.SetCreationTime(file.FullName, new DateTime(1970, 1, 1));
         File.SetLastAccessTime(file.FullName, new DateTime(1970, 1, 1));
         File.SetLastWriteTime(file.FullName, new DateTime(1970, 1, 1));
     }
 }

I'll be grateful for your help.


dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The error states the path is incorrect. I assume the user input (args[0]) is incorrect. Have you tried entering the same path in the windows explorer?

Anyway, I built a test based on your code with a removable disk and I do not receive a path error.

 static void Main(string[] args)
 {
     string path = args.Count() == 0 ? @"D:\test" : args[0];
    
     var files = new DirectoryInfo(path).GetFiles();
     Console.WriteLine(path);
    
     foreach (var file in files)
     {
         Console.WriteLine($"{file.FullName}: \t{File.GetCreationTime(file.FullName)}");
         File.SetCreationTime(file.FullName, new DateTime(1970, 1, 1));
         File.SetLastAccessTime(file.FullName, new DateTime(1970, 1, 1));
         File.SetLastWriteTime(file.FullName, new DateTime(1970, 1, 1));
         Console.WriteLine($"{file.FullName}: \t{File.GetCreationTime(file.FullName)}");
     }
 }




1 Vote 1 ·

0 Answers