C# Program to View the Access Date and Time of a File
Last Updated :
01 Nov, 2021
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.
Syntax:
file.CreationTime
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.
Syntax:
file.LastAccessTime
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.
Syntax:
file.LastWriteTime
It will return a DateTime which represents the time in the current file that was last written.
Approach
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
Example:
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:
C#
// C# Program to display the date
// and time of access of a file
using System;
using System.IO;
class GFG{
static void Main()
{
// Choose the file using file path
FileInfo path = new FileInfo("C://sravan//data.txt");
// Declare time variable using DateTime function
// This variable holds the time of the file in
// which it is created
DateTime createdtime = path.CreationTime;
// Get the file created time
Console.WriteLine("File is created at: {0}", createdtime);
// Get the file lastly accessed time
createdtime = path.LastAccessTime;
Console.WriteLine("File is accessed at lastly: {0}", createdtime);
// Get the file lastly updated/written time
createdtime = path.LastWriteTime;
Console.WriteLine("File is lastly written on: {0} ", createdtime);
}
}
Output:
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
Similar Reads
C# Program to Get File Time Using File Class Given a file, now our task is to get the file time using the File class. So we use the GetCreationTime() method of the File class. This method is used to find the creation date and time of the given file or directory. This method will only take one parameter that is the path of the file and if this
2 min read
C# Program to Check the Information of the File Given a file, now our task is to view the information of this file through C#. So to do this task we use FileInfo class. This class provides different types of properties and methods for creating, copying, deleting, opening, and moving files. It is also, used for creating FileStream objects. So here
1 min read
How to get file creation and modification date or time in Python? We often need to access various file properties for different purposes. Among the file properties, the creation and modification time of a file are the most commonly used ones. We can achieve the same functionality and use it for various utility purposes, using multiple methods. You can use function
4 min read
Dates and Times in Objective-C In Objective-C, dates and times are represented using the NSDate and NSDateFormatter classes. NSDate class represents a single point in time. It stores an absolute point in time, independent of any particular calendar or time zone. You can use NSDate to represent a date and time, or just a date or t
4 min read
How to Find Linux File Creation Time using Debugfs? Everything is treated as a file in Linux, and all the information about a file is stored in inodes, which includes the crucial metadata about a file such as creation time, last modification, etc. Every file in Linux is identified by its inode number. In this article, we will be using debugf command
2 min read
File Timestamps - mtime, ctime and atime in Linux Timestamps are records for the times in which actions are performed on files. A timestamp is useful because it keeps records of when a file was accessed, modified, or added. Linux's files have 3 timestamps recorded by the computer: Access timestamp (atime): which indicates the last time a file was a
4 min read