C# program to get last write time in UTC format of file or directory

In this C# program, we will figure out last write time in UTC format of file or directory? To get last write time in UTC, we are utilizing ‘GetLastWriteTimeUtc()’ technique for ‘Record’ class.

Given a document and an index and we need to get and print their last write time in UTC design utilizing C# program.

File.GetLastWriteTimeUtc()

This is a strategy for “Record class, which restores the Last Write time of a document or a registry in UTC design.

Syntax:

File.GetLastWriteTimeUtc(path);

Parameter(s):

path – Location of document or catalogue.

Last write time contains the following point of interest:

  • Date
  • Month
  • Year
  • Hour
  • Minute
  • Second
  • AM/PM

Program:

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            DateTime dt1;

            Console.WriteLine("Time before set last write time in UTC:");

            dt1 = File.GetLastWriteTimeUtc("ABC.TXT");
            Console.WriteLine("\tLast Write Time of file(ABC.TXT) in UTC: " + dt1);


            File.SetLastWriteTimeUtc("ABC.TXT", DateTime.Now);

            Console.WriteLine("Time After set last write time in UTC");

            dt1 = File.GetLastWriteTimeUtc("ABC.TXT");
            Console.WriteLine("\tLast Write Time of file(ABC.TXT) in UTC: " + dt1);


        }
    }
}

Output:

Time before set last write time in UTC:
        Last write Time of file(ABC.TXT) in UTC: 10/31/2017 4:25:36 PM
Time After set last write time in UTC
        Last write Time of file(ABC.TXT) in UTC: 10/31/2017 4:37:29 PM

Note: In the above program, we have to recollect, when we use “Record” class, System.IO namespace must be remembered for the program.

Leave a Comment

error: Alert: Content is protected!!