C# Program to View the Access Date and Time of a File

This is a C# Program to view the date and time of access of a file.

Problem Description

This C# Program Views the Date and time of Access of a File.

Problem Solution

Here the date and time of the file access are obtained using the info class functions.

Program/Source Code

Here is source code of the C# Program to View the Date and time of Access of a File. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to View the Date and time of Access of a File
 */
using System;
using System.IO;
class Program
{
    static void Main()
    {
        FileInfo info = new FileInfo("C:\\sri\\srip.txt");
        DateTime time = info.CreationTime;
        Console.WriteLine("File Creation Time     : {0}", time);
        time = info.LastAccessTime;
        Console.WriteLine("File Last Access Time  : {0}", time);
        time = info.LastWriteTime;
        Console.WriteLine("File Last Write Time   : {0} ", time);
        Console.Read();
    }
}
Program Explanation

This C# program is used to view the date and time of access of a file. The CreationTime is used to get or set the creation time of the current file or directory. Then LastAccessTime is used to get or set the time of the current file or directory last accessed.

advertisement
advertisement

The LastWriteTime is used to get or set the time when the current file or directory was last written to. The date and time of the file access is obtained using the info class functions and print the date and time access of a file.

Runtime Test Cases
 
File Creation Time   : 8/11/2013 7:17:20 PM
File Access Time     : 8/15/2013 1:08:45 PM
File Last Write Time : 8/15/2013 1:37:36 PM

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.