C# - How to Set New File's Creation Time?

learn how to set (define) a new file creation time using C# program?
Submitted by IncludeHelp, on October 29, 2017 [Last updated : March 26, 2023]

Given a file, and we have to define (set) a new file creation time using C# program.

To set new file's creation time in C#, we use File.SetCreationTime() method.

File.SetCreationTime()

This is a method of "File" class, which is used to define a new file creation time.

Syntax

void SetCreationTime(path, DateTime);

Parameter(s)

  1. path - Filename with its location.
  2. DateTime - Here we need to pass object of date time class..

Object of DateTime contains following information:

  1. Month
  2. Date
  3. Year
  4. Hour
  5. Minutes
  6. Seconds
  7. AM and PM

C# program to set new file creation time

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      DateTime D1 = File.GetCreationTime("XYZ.TXT");
      Console.WriteLine("Old File Creation Time : " + D1.ToString());

      DateTime D2 = new DateTime(2017, 12, 25, 19, 45, 10);

      File.SetCreationTime("XYZ.TXT", D2);

      DateTime D3 = File.GetCreationTime("XYZ.TXT");
      Console.WriteLine("New File Creation Time : " + D3.ToString());
    }
  }
}

Output

Old File Creation Time : 10/27/2017 10:45:10 AM
New File Creation Time : 12/25/2017 7:45:10 PM

Explanation

In the above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

C# File Handling Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.