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

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

提交回复
热议问题