Javatpoint Logo
Javatpoint Logo

File.GetLastWriteTimeUtc() method in C#

The GetLastWriteTimeUtc() method in C# returns a DateTime object representing the date and time a file was last written to, in Coordinated Universal Time (UTC). This allows obtaining the last modified timestamp of a file in UTC format.

Some important details regarding the File.GetLastWriteTimeUtc() function:

  • It belongs to the File class and is a static method in the System.IO namespace. Therefore, you would use summon it.

File.GetLastWriteTimeUtc(filePath) = DateTime lastWriteTimeUtc;

  • The parameter it takes is the path to the File as a string.
  • It returns a DateTime object representing the last write time in UTC, not local time. You need to convert it using TimeZoneInfo to get the time in local time.
  • If the File does not exist, it will throw a FileNotFoundException.
  • It does not take into account file attributes like LastAccessTime. It specifically returns the last time the file contents were written to.
  • You need sufficient permissions to use this method to read the File's metadata.

Here is an example of usage:

It gets the UTC last write time for the File at C:\Reports\report.pdf and then converts it to the local time zone before displaying it.

The File.GetLastWriteTimeUtc() method is useful in cases where you need to know precisely when a file was last modified. For example:

  • To determine if a file has changed since your program's last analysis.
  • To check if a generated report is up to date compared to the underlying data files.
  • To determine if a configuration file has been manually edited.
  • To compare file change times during synchronization processes.

It avoids ambiguity with local time zones or daylight savings time because it returns the exact UTC.

Some things to be careful of when using File.GetLastWriteTimeUtc():

  • It may not be what you expect on systems with inconsistent clock settings. The last write time comes from the file metadata.
  • There can be precision limitations on some file systems. For example, FAT32 only stores date and time up to 2 seconds.
  • Writing to a file does not always change the last write time, depending on how it is done.
  • The last write time can be manually changed without the contents being changed.
  • Other processes or threads accessing the File may cause the last write time to change unexpectedly.
  • You need to handle the possibility of the File not existing and catching the FileNotFoundException.

Overall, File.GetLastWriteTimeUtc() is a useful method for understanding when a file was last modified. Just be aware of its exact behaviour and the general limitations of File last write time tracking. Using UTC times for File modification tracking is often preferable to avoid time zone complexities.

Program:

Explanation of the above Program

  1. Declare and initialize the filePath string variable.
    • Declare a string variable to store the file path.
    • Initialize it with the full path of the File you want to check.
  2. Call the GetLastWriteTimeUtc() method.
    • Call the static GetLastWriteTimeUtc() method on the File class.
    • Pass the filePath string as the parameter.
    • It will return a DateTime representing the UTC last write time of the File.
  3. Convert returned UTC to the local time zone.
    • The returned DateTime is in UTC.
    • Use TimeZoneInfo.ConvertTimeFromUtc() to convert to the local time zone.
    • Pass the utcLastWriteTime and TimeZoneInfo.Local.
    • Saves result to localLastWriteTime DateTime.
  4. Print out UTC and local last write times.
    • Uses Console.WriteLine() to print the utcLastWriteTime and localLastWriteTime.
    • Prints in a readable format with labels.
  5. Calculate a TimeSpan of 7 days.
    • Use TimeSpan.FromDays() and pass 7 to create a 7-day timespan.
    • This gives a timespan representing 7 days.
  6. Check if file write time is within the last 7 days.
    • Subtract localLastWriteTime from current DateTime.Now.
    • Compare the result to the 7-day TimeSpan.
    • If less than 7 days, the File was recently written.
    • Else, the File was not written within the last 7 days.
  7. Take appropriate action based on file age check.
    • Use if/else blocks to take different actions.
    • g. Ignore old files, process recent files differently.
  8. Handle exceptions.
    • GetLastWriteTimeUtc() throws FileNotFoundException.
    • Wrap call in try/catch block to handle it gracefully.
    • It can also catch other IOExceptions.

It covers the key steps in detail with explanations of the main parts of the code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA