Method steps for C to manipulate directories and files

  • 2020-05-12 03:09:54
  • OfStack

The & # 8226; Create directories and files
1. You can merge paths through the Combine method of the Path class.

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");

2. Directory creation.
When creating a directory, if the directory already exists, the directory is not recreated and no errors are reported. Directories that do not exist at all levels of the path are automatically created when creating a directory.
(1) create by CreateDirectory method of Directory class.

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);

(2) object creation via DirectoryInfo.

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();

3. File creation.
Creating a file through the Create method overrides an existing file with the same name. When a file is created, the directory in which the file is located must exist, otherwise an error is reported.
(1) created by the Create method of the File class.

string activeDir = @"C:\myDir";
            string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
            System.IO.Directory.CreateDirectory(newPath);
            // create 1 Two blank files 
            string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
                + ".txt";
            string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
            System.IO.File.Create(filePathOne);

(2) create by FileInfo object.

// through Combine Merge directory 
            // And then create the directory 
            string activeDir = @"C:\myDir";
            string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
            System.IO.Directory.CreateDirectory(newPath);
            // create 1 Two blank files 
            string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
                + ".txt";
            string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
            System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne);
            fi.Create();

The & # 8226; Copy directory file

// Copies a single file to the specified directory 
            string fileName = "test.txt";
            string sourcePath = @"C:\testDir\subTestDir";
            string targetPath = @"C:\testDir\subTestDirTwo";
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);
            if (!System.IO.Directory.Exists(targetPath))
                System.IO.Directory.CreateDirectory(targetPath);
            // If it already exists, the parameter is false Error will be reported when the parameter is true Overwrite the file 
            // when copy When the method is two parameters, the default is overwritten as false . 
            System.IO.File.Copy(sourceFile, destFile, true);
            // Here is the copy 1 All files in the specified directory 
            // If you copy all the files in a directory with subdirectories, you can do this using a recursive or stack algorithm 
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);
                foreach (string s in files)
                {
                    // Returns only the filename and suffix of the path string 
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
        }

The & # 8226; Move directories and files

/* Move files */
            string sourceFile = @"C:\testDir\subTestDir\test.txt";
            string destFile = @"C:\testDir\subTestDirTwo\test.txt";
            // Throws an exception when the target file exists 
            System.IO.File.Move(sourceFile, destFile);
            /* Mobile directory */
            // Moving directories moves subdirectories and files of the changed directory 
            System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");

The & # 8226; Delete directories and files
1. Delete the directory
Deletes a directory and throws an exception if it does not exist. You can delete a directory either through the Delete method of the File class or through the FileInfo object method.
(1) delete directories through the Delete method of the File class

// Delete writable empty directory 
            // Throw a non-empty directory exception if it is not empty 
            try
            {
                System.IO.Directory.Delete(@"C:\testDir\subTestDir");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
            // The first 2 Parameters for false , you can only delete the empty directory, otherwise an exception is thrown that is not empty 
            // The first 2 Parameters for true When, delete the directory, including subdirectories and files 
            try
            {
                System.IO.Directory.Delete(@"C:\testDir\subTestDir", true);
            }
            catch(System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }

(2) delete the directory through the FileInfo object method

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
            try
            {
                // Delete an empty directory with no arguments 
                // When the parameter is false, Empty directories can be deleted ; for true , delete directories including subdirectories and files 
                di.Delete(true);
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }

2. Delete files
When deleting a file, no exception is thrown if the specified file directory exists and the file does not, or if the specified file directory does not exist, an exception is thrown.
(1) delete the file by File class Delete method

try
                {
                    System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
                }
                catch(System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                }

(2) delete the file by FileInfo object Delete method

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
0


Related articles: