Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

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
0
ba272
Asked:
ba272
  • 3
  • 3
1 Solution
 
Fahad MukhtarDistinguished EngineerCommented:
see System.IO.File.SetCreationTime() function
0
 
Fahad MukhtarDistinguished EngineerCommented:
example :
System.IO.File.SetCreationTime(@"C:\New File.txt", DateTime.Now);
0
 
ba272Author Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fahad MukhtarDistinguished EngineerCommented:
i think you are looking for last modified time instead of creation time...:

//Set Creation time for a file
System.IO.File.SetCreationTime(@"C:\New File.txt", DateTime.Now);
//Set last modified time (see that in the exploree window)
System.IO.File.SetLastWriteTime(@"C:\New File.txt", DateTime.Now);

right click the file icon and see the last created and modified properties..
0
 
ba272Author Commented:
Thanks a bunch.  It works perfect.
0
 
Thandava VallepalliCommented:
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
0
 
ba272Author Commented:
Thanks.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now