C# program to set last write time of file or directory

In this C# program, we will figure out how to set the last compose time of a record or a catalogue? To set this, we are utilizing ‘SetLastWriteTime()’ of ‘Document’ class.

Given a document and we need to set its last compose time utilizing C# program.

File.SetLastWriteTime()

This is a strategy for “Document class, it is utilized to set last compose time of a record/catalog.

Syntax:

File.SetLastWriteTime(path);

Parameter(s):

path – Location of document or catalog.

We can set after detail of last compose time:

  • 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:");
           
            dt1 = File. SetLastWriteTime("ABC.TXT");
            Console.WriteLine("\tLast write Time of file(ABC.TXT) : " + dt1);

            
            File. SetLastWriteTime("ABC.TXT", DateTime.Now);
           
            Console.WriteLine("Time After set last write time:");
           
            dt1 = File. SetLastWriteTime("ABC.TXT");
            Console.WriteLine("\tLast write Time of file(ABC.TXT) : " + dt1);

           
        }
    }
}

Output:

Time before set last write time:
        Last write  Time of file(ABC.TXT) : 10/31/2017 9:50:59 PM
Time After set last write  time:
        Last write  Time of file(ABC.TXT) : 10/31/2017 9:55:36 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!!