Shane
Shane

Reputation: 542

Unable to move files from a listbox to a newly created folder

I'm having issues moving files from a listbox to a newly created folder. I get: Cannot create a file when that file already exists.

public void CreateFolders()
    {            
        //create folders
        string folder1 = pattern.Substring(0, 2);
        string folder2 = pattern.Substring(3, 2);
        string folder3 = pattern.Substring(6, 2);
        Directory.CreateDirectory("c:\\destinationfolder" + "\\" + folder1);
        Directory.CreateDirectory("c:\\destinationfolder" + "\\" + folder1 + "\\" + folder2);
        Directory.CreateDirectory("c:\\destinationfolder" + "\\" + folder1 + "\\" + folder2 + "\\" + folder3);
        var destinationDirectoryFinal = Directory.CreateDirectory("c:\\destinationfolder" + "\\" + folder1 + "\\" + folder2 + "\\" + folder3);
        destinationDirectory = destinationDirectoryFinal.FullName.ToString();
    }

    public void MoveFiles()
    {
        try
        {
            //Move files from listbox to newly created folders
            foreach (string files in listBox1.Items)
            {
                File.Move(files, destinationDirectory);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }

    }

Upvotes: 0

Views: 30

Answers (1)

Rufus L
Rufus L

Reputation: 37020

The problem you're running into here is that you're trying to move a file to a directory that already contains a file with that name.

There are a couple of options you can choose from:

Option One

Check if the file exists before attempting the Move

foreach (var file in listBox1.Items)
{
    // Only move the file if it doesn't already exist
    if (!File.Exists(Path.Combine(destinationDirectory, Path.GetFileName(file))))
    {
        File.Move(file, destinationDirectory);
    }
}

Option Two

Always overwrite the file if it exists. We can do this in a two step process - first by calling File.Copy with the "overwrite" parameter set to true, and then by calling File.Delete to delete the file in the original location:

foreach (var file in listBox1.Items)
{
    // If the destination file already exists, overwrite it. Then delete the original
    File.Copy(file, Path.Combine(destinationDirectory, Path.GetFileName(file)), true);
    File.Delete(file);
}


Note: Another thing you can do to prevent errors (in either case) is to ensure that the source file exists, and that the source directory is not the same as the destination directory before you try anything:

foreach (var file in listBox1.Items)
{
    // Ensure that the file exists and that the source 
    // and destination directories are not the same
    if (!(File.Exists(file)) ||
        Path.GetDirectoryName(file).Equals(
            destinationDirectory, StringComparison.OrdinalIgnoreCase))
    {
        continue; // Continue to the next loop iteration without doing anything
    }

    // Rest of loop code here...
}

Upvotes: 1

Related Questions