SoFunction
Updated on 2025-03-07

C# uses FileInfo class to implement copying files

The example described in this article is C# using the FileInfo class to copy files. In the program, C# first creates a StreamWriter object writer, which appends text to the file represented by srcFile in the instance of FileInfo, and the CopyTo method of the FileInfo class to realize the copying of the file.

The specific implementation code is as follows:

using System;
using ;
namespace Copy the file
{
 class Class1
 {
 [STAThread]
 static void Main(string[] args)
 {
  FileInfo srcFile = new FileInfo(@"E:\Temp\");
  // Create a StreamWriter object writer that appends text to the file represented by srcFile instance of FileInfo.  StreamWriter writer = ();
  ("This instance implements copying of files");
  // Clean all buffers of the current writer and write all buffered data to the underlying stream  ();
  ();
  // Create a StreamReader that uses UTF8 encoding to read from an existing text file  StreamReader reader = ();
  ("The source file is:");
  // The Peek method returns the next available character, and if the available characters exist, it returns a non-negative integer.  while ( () >= 0)
  {
  (());
  }
  FileInfo desFile = new FileInfo("E:\\Temp\\");
  // CopyTo method of FileInfo class implements copying of files  FileInfo hello = ("E:\\Temp\\", true);
  reader = ();
  ("The copy after copy is:");
  while (()>= 0)
  {
  (());
  }
  ();
 }
 }
}