SoFunction
Updated on 2025-03-08

How to operate directories and files in C#

• Create directories and files
1. The path can be merged through the Combine method of the Path class.
Copy the codeThe code is as follows:

string activeDir = @"C:\myDir";
string newPath = (activeDir, "mySubDirOne");

2. Creation of directories.
When creating a directory, if the directory already exists, the directory will not be recreated and there will be no errors. When creating a directory, directories that do not exist at all levels in the path are automatically created.
(1) Created through the CreateDirectory method of the Directory class.
Copy the codeThe code is as follows:

string activeDir = @"C:\myDir";
string newPath = (activeDir, "mySubDirOne");
(newPath);

(2) Create through DirectoryInfo object.
Copy the codeThe code is as follows:

di = new (@"C:\myDirTwo\mySubDirThree");
();

3. Creation of files.
Create a file through the Create method, and the existing file with the same name will be overwritten. When creating a file, the directory where the file is located must exist, otherwise an error will be reported.
(1) Create through the Create method of the File class.
Copy the codeThe code is as follows:

string activeDir = @"C:\myDir";
            string newPath = (activeDir, "mySubDirOne");
            (newPath);
//Create a blank file
            string fileNameOne = ("yyyyMMddHHmmssffff")
                + ".txt";
            string filePathOne = (newPath, fileNameOne);
            (filePathOne);

(2) Created through FileInfo object.
Copy the codeThe code is as follows:

//Merge the directory through Combine
//Then create the directory
            string activeDir = @"C:\myDir";
            string newPath = (activeDir, "mySubDirOne");
            (newPath);
//Create a blank file
            string fileNameOne = ("yyyyMMddHHmmssffff")
                + ".txt";
            string filePathOne = (newPath, fileNameOne);
            fi = new (filePathOne);
            ();

• Copy directory files
Copy the codeThe code is as follows:

//Copy a single file to the specified directory
            string fileName = "";
            string sourcePath = @"C:\testDir\subTestDir";
            string targetPath = @"C:\testDir\subTestDirTwo";
            string sourceFile = (sourcePath, fileName);
            string destFile = (targetPath, fileName);
            if (!(targetPath))
                (targetPath);
//If it already exists, an error will be reported when the parameter is false, and the parameter is true to rewrite the file
//When the copy method is two parameters, the default is rewrite to false.
            (sourceFile, destFile, true);
//The following is to copy all files in a directory to the specified directory
//If you copy all files in a directory with a subdirectory, you can use recursive or stack algorithm to implement it
            if ((sourcePath))
            {
                string[] files = (sourcePath);
                foreach (string s in files)
                {
//Return only the file name and suffix of the path string
                    fileName = (s);
                    destFile = (targetPath, fileName);
                    (s, destFile, true);
                }
            }
        }

• Move directories and files
Copy the codeThe code is as follows:

/*Move file*/
            string sourceFile = @"C:\testDir\subTestDir\";
            string destFile = @"C:\testDir\subTestDirTwo\";
// When the target file exists, an exception is thrown
            (sourceFile, destFile);
/*Mobile directory*/
//Move directory will move the subdirectories and files of the directory
            (@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");

• Delete directories and files
1. Delete the directory
Delete the directory, and if the directory does not exist, an exception will be thrown. You can delete the directory through the Delete method of the File class, or you can delete the directory through the FileInfo object method.
(1) Delete the directory through the Delete method of the File class
Copy the codeThe code is as follows:

//Delete empty directory
//If the directory is not empty, throw an exception
            try
            {
                (@"C:\testDir\subTestDir");
            }
            catch ( e)
            {
                ();
            }
//When the second parameter is false, you can only delete the empty directory, otherwise an unemployment exception will be thrown.
//When the second parameter is true, delete the directory, including subdirectories and files
            try
            {
                (@"C:\testDir\subTestDir", true);
            }
            catch( e)
            {
                ();
            }

(2) Delete the directory through the FileInfo object method
Copy the codeThe code is as follows:

di = new (@"C:\testDir\subTestDirTwo");
            try
            {
//Delete empty directory without parameters
//When the parameter is false, the empty directory can be deleted; if true, delete the directory, including subdirectories and files
                (true);
            }
            catch ( e)
            {
                ();
            }

2. Delete the file
When deleting a file, if the directory of the specified file exists but the file does not exist, an exception will not be thrown. If the directory of the specified file does not exist, an exception will be thrown.
(1) Delete file through File class Delete method
Copy the codeThe code is as follows:

try
                {
                    (@"C:\testDir\subTestDir\");
                }
                catch( e)
                {
                    ();
                }

(2) Delete the file through the FileInfo object Delete method
Copy the codeThe code is as follows:

fi = new (@"C:\testDir\subTestDir\");
            try
            {
                ();
            }
            catch( e)
            {
                ();
            }