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

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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" error. Below is my code.

could any one suggest on this?

        string sourcedirectory = @"F:\source";         string destinationdirectory = @"F:\destination";          try         {             if (Directory.Exists(sourcedirectory))             {                 if (Directory.Exists(destinationdirectory))                 {                   Directory.Move(sourcedirectory, destinationdirectory);                 }                 else                 {                   Directory.CreateDirectory(destinationdirectory);                   Directory.Move(sourcedirectory, destinationdirectory);                 }             }          }         catch (Exception ex)         {             log(ex.message);         }

回答1:

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();     } }


回答2:

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



回答3:

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"."



回答4:

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);


回答5:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!