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

后端 未结 5 929
我寻月下人不归
我寻月下人不归 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

提交回复
热议问题