Javatpoint Logo
Javatpoint Logo

File.SetLastWriteTimeUtc() Method in C#

In this article, you will learn about the File.SetLastWriteTimeUtc() Method in C# with their syntax, parameters, and example.

What is the File.SetLastWriteTimeUtc() Method?

The last write time of a file may be set to a given UTC (Coordinated Universal Time) value in C# using the "File.SetLastWriteTimeUtc" method. This method is a part of the System.IO namespace's File class. It allows you to modify a file's last write time attribute, indicating when it was last modified or written to.


It has the following syntax:

  • path: It is the path of the File whose last write time you wish to set.
  • lastWriteTimeUtc: The UTC time to set as the last write time for the File.


  1. UnauthorizedAccessException: In this exception, the caller does not possess the required permission to do the task.
  2. ArgumentException: The path is invalid characters, all-white spaces, or an empty string.
  3. PathTooLongException: The file name, path, or both exceed the maximum length set by the System.
  4. NotSupportedException: File system last write time changes are not supported.

Key Points for the File.SetLastWriteTimeUtc() Method:

There are several key points for the File.SetLastWriteTimeUtc() method in C#. Some main points are as follows:

  1. The SetLastWriteTimeUtc method allows you to explicitly set the last write time for a file to a specific UTC time.
  2. The path parameter specifies the File that the method operates on.
  3. This method modifies the date of an existing file; it does not create the File if it does not already exist.
  4. Proper exception handling is crucial to deal with potential issues during the operation, such as insufficient permissions or unsupported file systems.
  5. This method is commonly used in scenarios where accurate and consistent file timestamp information is essential, especially in applications dealing with File versioning, synchronization, or auditing.


Let us take an example to illustrate the File.SetLastWriteTimeUtc method in C#.


An error occurred: Could not find file '/home/compiler/sample.txt'.


In this example, the last write time of the file "sample.txt" is set to the current UTC time using the File.SetLastWriteTimeUtc method. The current UTC time is provided via the DateTime.UtcNow function.

It is crucial to handle exceptions correctly since modifying file attributes can lead to several difficulties, such as invalid paths, permission issues, or unsupported file systems. The try-catch block in the example demonstrates a basic error-handling approach.

1. Setting Custom Last Write Time

Let us take another example to illustrate the File.SetLastWriteTimeUtc() method using custom last write time in C#.


An error occurred: Could not find file '/home/compiler/samplefile.txt'.


This code explains how to use File to establish a custom UTC last write time for a file (in this example, samplefile.txt).SetLastWriteTimeUtc and manage any errors that can arise along the way. Proper exception handling is important for handling unexpected situations and ensuring the robustness of your application.

2. Permission Issue


An error occurred: Could not find file '/home/compiler/samplefile.txt'.


In this example, the code uses the File.SetAttributes to simulate a read-only file and then try to set the latest write time; this code shows how to handle such cases. Proper exception handling is crucial for managing unexpected situations and ensuring the reliability of your application.

3. Handling Exceptions


File not found: Could not find file '/home/compiler/samplefile.txt'.


This code explains how to set a file's last write time, manages particular and generic exceptions that may arise during the process, and provides informative messages depending on the outcome.

Purpose and Usage

There are several purposes and usage of the File.SetLastWriteTimeUtc() Method in C#. Some main uses of this method are as follows:

  1. Update Timestamps: The main goal is to bring a file's latest write time up to a specific UTC value, which indicates when the File was last edited.
  2. Cross-Platform Timestamp Management: It is beneficial in situations when maintaining a consistent file timestamp across many systems and time zones is necessary.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA