Copy, delete, move, get file version information, change file attributes, delete read-only files

Source: Internet
Author: User

Copy a file:
Fileinfo fimyfile = new fileinfo (@ "C: \ 123 \ 456.txt ");
If (fimyfile. exists)
{
Fimyfile. copyto (@ "D: \ 123 \ 456.txt", true );
}

Delete an object:
Fileinfo fimyfile = new fileinfo (@ "C: \ 123 \ 456.txt ");
If (fimyfile. exists)
{
Fimyfile. Delete ();
}

// Copy an object
File. Copy (orignfile, newfile );
// Delete an object
File. Delete (delfile );
// Move the file
File. Move (orignfile, newfile );

If (file. exists (orignfile ))
{
Fileinfo Fi = new fileinfo (newfile );
Datetime ctime = Fi. creationtime;
}

// ================================================ ====================================

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 );

Determine whether the file exists: file. exists (string filepath)

Determine whether the directory exists: directory. exists ("D: \ lastestversion ")

Read files by row:

Int filecount = 0;
// Open the file just specified such that no one else can use it.
Streamreader sr = new streamreader (textbox1.text. Trim ());
While (Sr. Peek ()>-1) // streamreader. Peek () returns the next available character, but does not use it
{
Listbox1.items. Add (Sr. Readline ());
Filecount ++;
}
Sr. Close ();

Write files by row:

Streamwriter Sw = new streamwriter ("d :\\ result.txt ");
For (INT I = 0; I <10; I ++)
{
Sw. writeline ("this is the" + I. tostring () + "row data ");
}

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.