SoFunction
Updated on 2025-03-06

C# file operations (moving, copying, renaming)

C# file operations (moving, copying, renaming)

Updated: December 21, 2020 09:27:29 Author: yangyang
This article mainly introduces how to operate C# files (move, copy, rename), helping everyone better understand and use C#. Interested friends can learn about it

File movement

public static void MoveFolder(string sourcePath, string destPath)
    {
      if ((sourcePath))
      {
        if (!(destPath))
        {
          //Create the target directory if it does not exist          try
          {
            (destPath);
          }
          catch (Exception ex)
          {
            throw new Exception("Creating the target directory failed:" + );
          }
        }
        //Get all files under the source file        List<string> files = new List<string>((sourcePath));
        (c =>
        {
          string destFile = (new string[] { destPath, (c) });
          //Coverage mode          if ((destFile))
          {
            (destFile);
          }
          (c, destFile);
        });
        //Get all directory files under the source file        List<string> folders = new List<string>((sourcePath));

        (c =>
        {
          string destDir = (new string[] { destPath, (c) });
          //It must be moved in the same root directory to be effective, and it cannot be moved in different volumes.          //(c, destDir); 

          //Use recursive method to implement          MoveFolder(c, destDir);
        });
      }
      else
      {
    

File Copy

public static void CopyFilefolder(string sourceFilePath, string targetFilePath)
    {
      //Get all non-directory files in the source folder      string[] files = (sourceFilePath);
      string fileName;
      string destFile;
      //If the target folder does not exist, create a new target folder      if (!(targetFilePath))
      {
        (targetFilePath);
      }
      //Copy the obtained files to the target folder one by one      foreach (string s in files)
      {
        fileName = (s);
        destFile = (targetFilePath, fileName);
        (s, destFile, true);
      }
      //The above paragraph can be seen on MSDN
      //Get and store the folder name in the source folder      string[] filefolders = (sourceFilePath);
      //Create Directoryinfo instance      DirectoryInfo dirinfo = new DirectoryInfo(sourceFilePath);
      //Get all subfolders in the source folder      DirectoryInfo[] subFileFolder = ();
      for (int j = 0; j < ; j++)
      {
        //Get all subfolder names        string subSourcePath = sourceFilePath + "\\" + subFileFolder[j].ToString();
        string subTargetPath = targetFilePath + "\\" + subFileFolder[j].ToString();
        //Recall the obtained subfolder as a new source folder and call CopyFilefolder recursively        CopyFilefolder(subSourcePath, subTargetPath);
      }
    }

File renaming

public ExecutionResult FileRename(string sourceFile, string destinationPath, string destinationFileName)
    {
      ExecutionResult result;
      FileInfo tempFileInfo;
      FileInfo tempBakFileInfo;
      DirectoryInfo tempDirectoryInfo;

      result = new ExecutionResult();
      tempFileInfo = new FileInfo(sourceFile);
      tempDirectoryInfo = new DirectoryInfo(destinationPath);
      tempBakFileInfo = new FileInfo(destinationPath + "\\" + destinationFileName);
      try
      {
        if (!)
          ();
        if ()
          ();
        //move file to bak
        (destinationPath + "\\" + destinationFileName);

         = true;
         = "Rename file OK";
         = "OK";
      }
      catch (Exception ex)
      {
         = false;
         = "Mail";
         = ;
        if ()
        {
          (().Name, "Rename file error. Msg :" + );
          ();
        }
      }

      return result;
    }

The above is the detailed content of c# file operations (moving, copying, renaming). For more information about c# file operations, please follow my other related articles!

  • c#
  • document
  • move
  • copy
  • Rename

Related Articles

  • C# XML basics summary (retrieve the content of XML files)

    This article mainly introduces the basic introduction to C# XML (add, delete, modify and clarify the content of XML files). The example code is introduced in this article in detail, which has a certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2022-04-04
  • Dynamically generate a mini program QR code with parameters based on C#

    In the WeChat mini program management background, we can generate and download standard mini program QR codes and provide the main program entry function. In actual application development, mini program QR codes can carry parameters and can be generated dynamically. The editor of this article will introduce to you how to dynamically generate mini program QR codes with parameters based on C#. Interested friends can refer to the following
    2023-12-12
  • C# uses ZBar to identify barcodes

    Currently, the mainstream recognition libraries are mainly ZBar. This article mainly introduces how to use ZBar library to realize the barcode recognition function. The sample code in the article is explained in detail. Interested friends can learn about it.
    2023-07-07
  • C# socket network programming sample code for receiving and sending data

    This article mainly introduces C# socket network programming, server side receiving, client side sending data, please refer to it.
    2013-12-12
  • C# implements sample code for converting RTF to HTML

    RTF documents are documents in rich text formats (Rich Text Format). When we are processing files, when we need to convert the document format, we can convert RTF to other formats, such as DOCX/DOC, PDF or HTML. This article will use C# to implement RTF to HTML. If you need it, please refer to it.
    2022-04-04
  • C# instance method to connect to Oracle database

    C# instance method to connect to Oracle database, friends who need it can refer to it
    2013-04-04
  • C# implementation sets expiration time for PDF documents

    We can set expiration time and expiration information prompts for some important documents or temporary files to remind readers or administrators of the timeliness of documents, and adjust and update documents in a timely manner. The following article will introduce how to set the expiration time for PDF documents through C#. If you need it, please refer to it
    2022-01-01
  • Locking system in C# multi-threaded programming (III)

    This article mainly introduces the lock system in C# multi-threaded programming (III). This article mainly talks about thread synchronization methods, events, semaphores, as well as WaitHandle, AutoResetEvent, ManualResetEvent and other contents based on kernel mode construction. Friends who need it can refer to it.
    2015-04-04
  • Detailed explanation of file upload operation of WinForm automatic update program

    This article mainly introduces the file upload operation in the C# WinForm automatic update program in detail. The sample code in the article is explained in detail and has certain reference value. Interested friends can learn about it.
    2022-10-10
  • C#.Net implements grayscale map and HeatMap heat map winform (advanced)

    This article mainly introduces the implementation of simple grayscale graphs and cool HeatMap heatmap winform in C#.NET. The example code is introduced in this article in detail and has certain reference value. Interested friends can refer to it.
    2021-12-12

Latest Comments