How to delete a read-only file in C #

Source: Internet
Author: User
How to delete a read-only file in C #
Get the file version information:
Fileversioninfo myfileversioninfo1 = fileversioninfo. getversioninfo ("D: \ test. dll ");
Textbox1.text = "version:" + myfileversioninfo1.fileversion;
Modify file attributes and delete read-only files:
In the following example, you want to copy the E: \ test.txt file to D: \ TMP \ test.txt, but D: \ TMP \ test.txt already exists.
// File. Copy (sourcefile, destinationfile, true); used to copy an object
// When destinationfile already exists, you cannot copy file file1 to the target file,
// Delete the destination file first. The file. Delete () method cannot delete the read-only file,
// Therefore, if the file attribute is read-only (the attributes attribute contains "readonly "),
// First reset the file property to normal and then delete the file:
String file1 = "E: \ test.txt ";
String destinationfile = "D: \ TMP \ test.txt ";
If (file. exists (destinationfile ))
{
Fileinfo Fi = new fileinfo (destinationfile );
If (Fi. Attributes. tostring (). indexof ("readonly ")! =-1)
Fi. Attributes = fileattributes. normal;
File. Delete (destinationfile );
}
File. Copy (file1, destinationfile, true );

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.