Below is a simple tool written in C# by Jianlu Office, which can batch modify the file creation time, last access time, and last modification time. We will use the .NET Framework or .NET Core to implement this functionality.
Complete sample code
1. Create a new C# console application
You can create a new console application using Visual Studio or the .NET CLI.
2. Write code
Open the file and replace its content with the following code:
using System; using ; using ; using ; namespace FileTimeModifier { class Program { static void Main(string[] args) { ("Jianlu Batch Modify File Time Attribute Tool"); // Get the folder path ("Please enter the folder path: "); string folderPath = (); if ((folderPath) || !(folderPath)) { ("Invalid folder path."); return; } // Get the file list List<string> files = (folderPath).ToList(); if ( == 0) { ("There is no file in the folder."); return; } // Get a new time ("Please enter a new date and time (Format: yyyy-MM-dd HH:mm:ss): "); string dateTimeString = (); if ((dateTimeString, out DateTime newDateTime)) { foreach (string file in files) { try { // Modify the file's time attributes (file, newDateTime); (file, newDateTime); (file, newDateTime); ($"Modified file {file} Time attributes。"); } catch (Exception ex) { ($"Modify the file {file} Time attributes时发生错误: {}"); } } } else { ("Invalid date and time format."); } ("Press any key to exit..."); (); } } }
Code description
Get the folder path:
The user enters the folder path, and the program will check whether the path exists.
Get the file list:
Use the method to get all files in the folder.
Get a new time:
When the user enters a new date and time, the program will check whether the input format is correct.
Modify file time attributes:
Use , and methods to set the file creation time, last access time and last modification time respectively.
Exception handling:
Catch and handle possible exceptions to ensure the robustness of the program.
Sample run
Suppose you have a folder C:\TestFolder with some files inside. After running the program, enter the folder path C:\TestFolder and the new date and time 2023-10-01 12:00:00. The program will batch modify the time properties of all files in the folder.
Of course, if you find it difficult to write yourself, you can also search and use the Jianlu file batch renaming tool. This software supports Windows and Mac systems, making it more convenient and quick to use.
This is the article about writing a widget that modifies file time attributes based on C#. For more related contents for modifying file time in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!