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
ba272Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.