Link to home
Start Free TrialLog in
Avatar of ba272
ba272

asked on

Setting the date and time for a file

Hi,

Back when I used Borland compilers, I used a function called, "touch( filename, datetime )", which would set the time and date of the file specified.

Does anybody know the equivalent in C#?

Thanks,
Bob
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

see System.IO.File.SetCreationTime() function
example :
System.IO.File.SetCreationTime(@"C:\New File.txt", DateTime.Now);
Avatar of ba272
ba272

ASKER

It looks fine, and it compiles.  But it doesn't work.  Neither the file time nor date changes.   And I close Windows Explorer first.  

Any ideas?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ba272

ASKER

Thanks a bunch.  It works perfect.
Try this code.... i have tried in my system its working fine....

=======================
using System.IO;

class Demo
{
      public static void Main()
      {
            File.SetCreationTime(@"f:\Book1.xls", DateTime.Now);
            File.SetLastAccessTime(@"f:\Book1.xls", DateTime.Now);
            File.SetLastWriteTime(@"f:\Book1.xls", DateTime.Now);
      }
}
========================

itsvtk
Avatar of ba272

ASKER

Thanks.