Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to touch a file in C#?

Tags:

c#

.net

file

In C#, what's the simplest/safest/shortest way to make a file appear as though it has been modified (i.e. change its last modified date) without changing the contents of the file?

like image 613
Matt Howells Avatar asked Nov 11 '09 16:11

Matt Howells


People also ask

How do you close a file in C?

The file (both text and binary) should be closed after reading/writing. Closing a file is performed using the fclose() function. fclose(fptr); Here, fptr is a file pointer associated with the file to be closed.

What is file handling in C?

What is File Handling in C? File handling refers to the method of storing data in the C program in the form of an output or input that might have been generated while running a C program in a data file, i.e., a binary file or a text file for future analysis and reference in that very program.

What is the need for closing a file in C language?

Closing a file clears Buffer contents from RAM or memory. C) Unclosed files occupy memory and PC hangs when on low memory.


1 Answers

System.IO.File.SetLastWriteTimeUtc(fileName, DateTime.UtcNow); 

If you don't know whether the file exists, you can use this:

if(!System.IO.File.Exists(fileName))      System.IO.File.Create(fileName).Close();   // close immediately   System.IO.File.SetLastWriteTimeUtc(fileName, DateTime.UtcNow) 
like image 163
Matt Howells Avatar answered Oct 07 '22 08:10

Matt Howells