Jey
Jey

Reputation: 2127

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

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

Upvotes: 27

Views: 81582

Answers (5)

BornToCode
BornToCode

Reputation: 10213

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.

Upvotes: 0

cuongle
cuongle

Reputation: 75306

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

Upvotes: 4

prashanth
prashanth

Reputation: 2099

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

Upvotes: 2

Mark Hall
Mark Hall

Reputation: 54532

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

Upvotes: 24

Vignesh.N
Vignesh.N

Reputation: 2666

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

Upvotes: 4

Related Questions