C # using the FileInfo class to implement the method of copying files

Source: Internet
Author: User
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 (); } }}

Related Article

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.