The best way to move a directory with all its contents(files or folders) in C#?

后端 未结 6 1835
萌比男神i
萌比男神i 2021-01-16 16:55

What is the best way to move directory with all its contents(files or folders) in C#?

I used the following codes, but it throws The Directory is not empty

相关标签:
6条回答
  • 2021-01-16 17:27

    You can try this

        DirectoryInfo dir = new DirectoryInfo(source);
        dir.MoveTo(newLocation);
    
    0 讨论(0)
  • 2021-01-16 17:27

    What about the following solution, it works well :

    public static void DeleteDirectory(string targetDir)
    {
        string[] files = Directory.GetFiles(targetDir, "*", SearchOption.AllDirectories);
        foreach (string file in files)
            File.Delete(file);
        new Microsoft.VisualBasic.Devices.Computer().FileSystem.DeleteDirectory(targetDir, DeleteDirectoryOption.DeleteAllContents);
    }
    
    public static void MoveDirectory(string source, string dest)
    {
        new Microsoft.VisualBasic.Devices.Computer().FileSystem.CopyDirectory(source, dest, true);
        DeleteDirectory(source);
    }
    
    0 讨论(0)
  • 2021-01-16 17:31

    here is a example ...you should move all files before move directory

     string fileName = "test.txt";
            string sourcePath = @"C:\Users\Public\TestFolder";
            string targetPath =  @"C:\Users\Public\TestFolder\SubDir";
    
            // Use Path class to manipulate file and directory paths. 
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);
    
            // To copy a folder's contents to a new location: 
            // Create a new target folder, if necessary. 
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
    
            // To copy a file to another location and  
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);
    
            // To copy all the files in one directory to another directory. 
            // Get the files in the source folder. (To recursively iterate through 
            // all subfolders under the current directory, see 
            // "How to: Iterate Through a Directory Tree.")
            // Note: Check for target path was performed previously 
            //       in this code example. 
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);
    
                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }
    
    0 讨论(0)
  • 2021-01-16 17:34

    MSDN example enough for this.

            string sourceDirectory = @"C:\source";
            string destinationDirectory = @"C:\destination";
    
            try
            {
                Directory.Move(sourceDirectory, destinationDirectory);  
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
    

    Even, as MSDN says, you will get an IOException if:

    • An attempt was made to move a directory to a different volume.

    • destDirName already exists.

    • The sourceDirName and destDirName parameters refer to the same file or directory.

    So, verify whether any of above is causing for error.

    Also, you might consider deleting existing directory before moving. This would avoid raising error if error is due to 2nd option mention above.

        if(Directory.Exists(destinationDirectory) == false)
        {
             Directory.Move(sourceDirectory, destinationDirectory);
        }
        else
        {
             //Delete existing directory
             //But make sure you really want to delete.
        }
    
    0 讨论(0)
  • 2021-01-16 17:38

    Use System.Io.

    string source = @"c:\new";
    string dest = @"c:\newnew";
    Directory.Move(source, dest);
    
    0 讨论(0)
  • 2021-01-16 17:44

    Use this would help you:

            string sourceDir = @"c:\test";
            string destinationDir = @"c:\test1";
    
            try
            {
                // Ensure the source directory exists
                if (Directory.Exists(sourceDir) == true )
                {
                    // Ensure the destination directory doesn't already exist
                    if (Directory.Exists(destinationDir) == false)
                    {
                        // Perform the move
                        Directory.Move(sourceDir, destinationDir);
                    }
                    else
                    {
                        // Could provide the user the option to delete the existing directory
                        // before moving the source directory
                    }
                }
                else
                {
                    // Do something about the source directory not existing
                }
            }
            catch (Exception)
            {
                // TODO: Handle the exception that has been thrown
            }
    
    0 讨论(0)
提交回复
热议问题