C# Program to View the Access Date and Time of a File
Given a file, our task is to view the date and time of access to a file. So to do this we use the following properties of the FileSystemInfo class:
1. CreationTime: This property is used to get the time in which the file is created.
Where the file is the path of the file and it will return DateTime. A DateTime structure is set to the date and time that the specified file.
2. LastAccessTime: This property is used to get the time in which the file is lastly used/accessed.
It will return a DateTime which represents the time in which the current file was accessed.
3. LastWriteTime: This property is used to get the time in which the file or directory is lastly written/updated.
It will return a DateTime which represents the time in the current file that was last written.
1. Read the file by using the file path i.e., C://sravan//data.txt
2. Declare DateTime variable for accessing file time details using the CreationTime property.DateTime createdtime = path.CreationTime
3. Get the file last access time using the LastAccessTime property.createdtime = path.LastAccessTime
4. Get the file lastly written time using the LastWriteTime property.createdtime = path.LastWriteTime
In this example, we are going to create a file in C drive with two lines of data and the file name is data.txt and the path is shown in the below image:
File is created at: 10/23/2021 10:02:10 AM File is accessed at lastly: 10/23/2021 10:20:00 AM File is lastly written on: 10/23/2021 10:17:03 AM