File.GetLastWriteTime() Method in C# with Examples
File.GetLastWriteTime(String) is an inbuilt File class method which is used to return the date and time the specified file or directory was last written to.
public static DateTime GetLastWriteTime (string path);
Parameter: This function accepts a parameter which is illustrated below:
- path: This is the specified file path.
- UnauthorizedAccessException: The caller does not have the required permission.
- ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters as defined by InvalidPathChars.
- ArgumentNullException: The path is null.
- PathTooLongException: The given path, file name, or both exceed the system-defined maximum length.
- NotSupportedException: The path is in an invalid format.
Return Value: Returns the date and time the specified file or directory was last written to.
Below are the programs to illustrate the File.GetLastWriteTime(String) method.
Program 1: Before running the below code, a file file.txt is created with some contents shown below:
The last write time for this file was 4/19/2020 4:43:24 AM.
Program 2: Before running the below code, a file was created shown below:
The last write time for this file was 4/3/2020 12:00:00 AM.