C # Easy to read, change file creation, modification, access time Z

Source: Internet
Author: User

Read file creation, modification, access time
FileInfo fi = new FileInfo ("C://test.txt");
Console.WriteLine (FI. Creationtime.tostring ());
Console.WriteLine (FI. Lastwritetime.tostring ());
Console.WriteLine (FI. Lastaccesstime.tostring ());

Change (set) file creation, modification, access time
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));

The namespaces for FileInfo and File are:

system.io================================================

As an example of file read-only and hidden properties, other properties, such as "Archive", "system", and so on, are similar methods.

Read-only and hide-from-file properties

FileInfo fi = new FileInfo ("C://test.txt");
if (FI. Attributes & fileattributes.readonly) = = fileattributes.readonly)
{
Console.WriteLine ("is read Only");
}
Else
{
Console.WriteLine ("Not read Only");
}
if (FI. Attributes & Fileattributes.hidden) = = Fileattributes.hidden)
{
Console.WriteLine ("is hidden");
}
Else
{
Console.WriteLine ("Not Hidden");
}

Set file read-only and hidden properties

FileInfo fi = new FileInfo ("C://test.txt");
Fi. Attributes = fi. Attributes | fileattributes.readonly | Fileattributes.hidden; Fayi
File.setattributes ("C://test.txt", fi. Attributes | fileattributes.readonly | Fileattributes.hidden); Law II

As you can see, you can either set the Attributes property or use the SetAttributes method of File to come to the end.

Note that, regardless of what to do, first get the original properties of the file, if you do not get the original properties of the file, is directly fileattributes.readonly | Fileattributes.hidden, then, some properties may be lost, such as the file may have an "archive" attribute, if this is set directly, the archive property is lost.

Canceling file read-only and hidden properties

FileInfo fi = new FileInfo ("C://test.txt");
Fi. Attributes = fi. Attributes & ~fileattributes.readonly & ~fileattributes.hidden; Fayi
File.setattributes ("C://test.txt", fi. Attributes & ~fileattributes.readonly & ~fileattributes.hidden); Law II

Similar to Settings, just some of the arithmetic symbols have changed. Note that the online argument is to set the property directly to Fileattributes.normal, which is not correct, which not only cancels the read-only and hidden properties, but may also cancel some other properties, such as "archive".

Description

Improper property settings may prevent files from appearing in Windows Explorer, but they do exist, and you can use Fileattributes.normal to display the files for easy operation.

C # Easy to read, change file creation, modification, access time Z

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.