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