Cannot create a file when that file already exists when using Directory.Move

后端 未结 5 918
我寻月下人不归
我寻月下人不归 2020-12-17 08:59

I am trying to move the directory from one location to another location on the same drive. I am getting \"Cannot create a file when that file already exists

相关标签:
5条回答
  • 2020-12-17 09:35

    You don't need to create Directory first, it will throw IO Exception, if destination directory exists, Move method automatically creates it for you:

    string sourcedirectory = @"F:\source";
    string destinationdirectory = @"F:\destination";
    
    if (Directory.Exists(sourcedirectory))
    {
        if (!Directory.Exists(destinationdirectory))
        {
             Directory.Move(sourcedirectory, destinationdirectory);
        }
    }
    

    More infomation of Directory.Move:

    http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx

    0 讨论(0)
  • 2020-12-17 09:37

    As both of the previous answers pointed out, the destination Directory cannot exist. In your code you are creating the Directory if it doesn't exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.

    Something like this:

    class Program
    {
        static void Main(string[] args)
        {
            string sourcedirectory = @"C:\source";
            string destinationdirectory = @"C:\destination";
            string backupdirectory = @"C:\Backup";
            try
            {
                if (Directory.Exists(sourcedirectory))
                {
                    if (Directory.Exists(destinationdirectory))
                    {
                        //Directory.Delete(destinationdirectory);
                        Directory.Move(destinationdirectory, backupdirectory + DateTime.Now.ToString("_MMMdd_yyyy_HHmmss"));
                        Directory.Move(sourcedirectory, destinationdirectory);
                    }
                    else
                    {
                        Directory.Move(sourcedirectory, destinationdirectory);
                    }
                }
    
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
    
    0 讨论(0)
  • 2020-12-17 09:42

    As per MSDN,

    This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists.

    But, in your method, you are creating the destination directory before you move.

    So, you need to change your method from

    if (Directory.Exists(destinationdirectory))
    {
      Directory.Move(sourcedirectory, destinationdirectory);
    }
    else
    {
      Directory.CreateDirectory(destinationdirectory);
      Directory.Move(sourcedirectory, destinationdirectory);
    }
    

    to

    if (Directory.Exists(destinationdirectory))
    {
      //delete or rename
    }
    Directory.Move(sourcedirectory, destinationdirectory);
    
    0 讨论(0)
  • 2020-12-17 09:46

    You can just call

    Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(source, destination, true);
    

    What it does internally is it creates the target directory if it's not exists and then it iterates over the source directory's files and moves them to the target directory. That way the problem of "Cannot create a file when that file already exists" won't happen.

    You'll need to add Microsoft.VisualBasic as a reference.

    0 讨论(0)
  • 2020-12-17 09:47

    from http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx

    "This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify "c:\public\mydir" as the destDirName parameter, provided that "mydir" does not exist under "c:\public", or specify a new directory name such as "c:\newdir"."

    0 讨论(0)
提交回复
热议问题