Example of c# modifying file creation, modification and access time

Time:2022-5-11
catalogue
  • File class
  • Examples
  • C FileInfo class signature
  • C FileInfo constructor
  • C FileInfo attribute
  • C FileInfo method
  • Examples

C# implements the creation, modification and access time methods of modifying files, mainly using the setcreationtime, setlastwritetime and setlastaccesstime methods of file class. Let’s first introduce the file class:

File class

Namespace: system IO

Assembly: system IO. FileSystem. dll

Provides static methods for creating, copying, deleting, moving, and opening a single file, and assists in creating FileStream objects.

The functions of setcreationtime, setlastwritetime and setlastaccesstime of file class

method effect
SetCreationTime  Modify the creation time of the file
SetLastWriteTime Modification time of modified file
SetLastAccessTime Modify the access time of the file

Examples

//Change (set) the creation, modification and access time of the file
     File.SetCreationTime("C:\\test.txt", DateTime.Now.AddYears(-1));
     File.SetLastWriteTime("C:\\test.txt", DateTime.Now.AddYears(-2));
     File.SetLastAccessTime("C:\\test.txt", DateTime.Now.AddYears(-3));

If you need to obtain the creation, modification and access time of a file, you need to use the three methods of FileInfo class: creationtime, lastwritetime and lastaccesstime. Let’s first introduce the FileInfo class.

FileInfo class is used to process files and their operations in C. It provides properties and methods for creating, deleting and reading files. It uses the streamwriter class to write data to a file. It is system Part of the IO namespace.

C FileInfo class signature


[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class FileInfo : FileSystemInfo

C FileInfo constructor

The following table contains the constructors for the FileInfo class.

Builder describe
FileInfo(String) It is used to initialize a new instance of the FileInfo class, which acts as a wrapper for the file path.

C FileInfo attribute

The following table contains the properties of the FileInfo class.

attribute describe
Attributes It is used to get or set the properties of the current file or directory.
CreationTime It is used to get or set the creation time of the current file or directory.
Directory It is used to get an instance of the parent directory.
DirectoryName It is used to obtain a string representing the full path of the directory.
Exists It is used to get a value indicating whether the file exists.
FullName It is used to get the full path of a directory or file.
IsReadOnly It is used to get or set a value that determines whether the current file is read-only.
LastAccessTime It is used to get or set the time when the current file or directory was last accessed.
Length It is used to get the size of the current file in bytes.
Name Used to get the file name.

C FileInfo method

The following table contains the methods of the FileInfo class.

method describe
AppendText() It is used to create a streamwriter that attaches text to the file represented by this FileInfo instance.
CopyTo(String) It is used to copy an existing file to a new file.
Create() It is used to create files.
CreateText() It is used to create a streamwriter that writes a new text file.
Decrypt() It is used to decrypt files encrypted by the current account using the encrypt method.
Delete() It is used to permanently delete files.
Encrypt() It is used to encrypt a file so that only the account used to encrypt the file can decrypt it.
GetAccessControl() It is used to get a filesecurity object that encapsulates access control list (ACL) entries.
MoveTo(String) It is used to move the specified file to a new specified location.
Open(FileMode) Used to open a file in the specified mode.
OpenRead() It is used to create a read-only FileStream.
OpenText() It is used to create a StreamReader with utf8 encoding that can be read from an existing text file.
OpenWrite() It is used to create a write only FileStream.
Refresh() It is used to refresh the state of the object.
Replace(String, String) It is used to replace the contents of the specified file with the file described by the current FileInfo object.
ToString() It is used to return the path as a string.

Examples


     FileInfo fi = new FileInfo("C:\\test.txt");
     Console.WriteLine(fi.CreationTime.ToString());
     Console.WriteLine(fi.LastWriteTime.ToString());
     Console.WriteLine(fi.LastAccessTime.ToString());

Do you know the access time and modification method of c#? How to get the creation, modification and access time of files? Hope to help you!

The above is the details of the example of the creation, modification and access time of c# modification file. For more information about the creation, modification and access time of c# modification file, please pay attention to other relevant articles of developeppaer!