SoFunction
Updated on 2025-04-06

.NET instance method to create, delete, and copy folders and their subfiles


 using System;

using ;

using ;

using ;

using ;


namespace WinFormsApp_OperateFileAndFolder

{

    public class OperateFileFolder

    {

        /// <summary>

/// Create folder

        /// </summary>

        public string CreateFolder(string argPath)

        {

            string returnStr = "";

            try

            {

                if ((argPath))

                {

returnStr = "This folder already exists";

 

                    return returnStr;

                }

                else

                {

                    dirinfo = (argPath);

returnStr = "Successfully created this folder! The creation time is: " + (argPath);

                }

            }

            catch (Exception ee)

            {

returnStr = "Processing failed! The reason for the failure is: " + ();

            }

            return returnStr;

        }
        

        /// <summary>

/// Recursively delete folders and files

        /// </summary>

        /// <param name="dir"></param>

        public void DeleteFolder(string dir)

        {

//Unmask After executing this method, you can keep the root folder (only all children in the directory are deleted)

//// Check whether the target directory ends with directory splitting characters, if not, add it

            //if (dir[ - 1] != )

            //    dir += ;

 

If ((dir)) //If this folder exists, delete it

            {

                foreach (string d in (dir))

                {

                    if ((d))

(d); //Delivery the files in it directly

                    else

DeleteFolder(d); //Recursively delete subfolders

                }

(dir); //Delete empty folder

(dir + "Folder deletion succeeded");

            }

            else

(dir + "This folder does not exist"); //If the folder does not exist, prompt

        }

         
        /// <summary>

/// Implement a static method to copy all the contents below the specified folder to the target folder

/// If the target folder is a read-only property, an error will be reported.

        /// </summary>

        public static void CopyDir(string srcPath, string aimPath)

        {

            try

            {

// Check whether the target directory ends with directory segmentation characters. If not, add it

                if (aimPath[ - 1] != )

                    aimPath += ;

// Determine whether the target directory exists or not, create a new one.

                if (!(aimPath)) (aimPath);

// Get the file list of the source directory, which is an array containing the file and directory paths

// If you point to the file below the copy target file without including the directory, please use the following method

                // string[] fileList = (srcPath);

                string[] fileList = (srcPath);

//Transfuse all files and directories

                foreach (string file in fileList)

                {

// First treat it as a directory. If this directory exists, recursively copy the files below the directory.

                    if ((file))

                        CopyDir(file, aimPath + (file));

// Otherwise, directly copy the file

                    else

                        (file, aimPath + (file), true);

                }

            }

            catch (Exception e)

            {

                (());

            }

        }


    }

}