The example described in this paper is the method of using FileInfo class to implement copy file in C #, in which C # first creates a StreamWriter object writer, which appends text to the file represented by the FileInfo instance srcfile, FileInfo method to implement a copy of the file.
The specific implementation code is as follows:
Using system;using system.io;namespace Copy file {class Class1 {[STAThread] static void Main (string[] args) { FileInfo src File = new FileInfo (@ "E:\Temp\src.txt"); Creates a StreamWriter object writer, which appends text to the file represented by the FileInfo instance srcfile. StreamWriter writer = Srcfile.appendtext (); Writer. WriteLine ("Copy of this example implementation file"); Cleans up all buffers for the current writer and writes all buffered data to the underlying stream writer. Flush (); Writer. Close (); Create StreamReader StreamReader reader = Srcfile.opentext () that uses UTF8 encoding to read from an existing text file; Console.WriteLine ("Source file is:"); The Peek method returns the next available character and returns a non-negative integer while (reader) if the available characters exist . Peek () >= 0) { Console.WriteLine (reader). ReadLine ()); } FileInfo desfile = new FileInfo ("E:\\temp\\des.txt"); The CopyTo method of the FileInfo class, which implements a copy of the file FileInfo hello = Srcfile.copyto ("E:\\temp\\des.txt", true); reader = Desfile.opentext (); Console.WriteLine ("Copy after copy is:"); while (reader. Peek () >= 0) { Console.WriteLine (reader). ReadLine ()); } Reader. Close (); } }}