SoFunction
Updated on 2025-03-06

C# implements file Move and Copy operations

File Move operation and file copying are methods frequently encountered in C# program development. The operation of files is realized based on the incoming source file address and target file address parameters. The implementation code is as follows:

Move operation code

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
            {}
        }
       

Copy operation code

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

This is all about this article about implementing C# files Move and Copy operations. I hope it will be helpful to everyone's learning and I hope everyone will support me more.