Why doesn't this below code move file from one location to other

string source = textbox1.text;
string destination = textBox2.Text;
bool exists = System.IO.Directory.Exists(source);
if (exists)
{
    // its create directory to destination
    System.IO.Directory.CreateDirectory(destination);
    //  when directory creates it moves it
    System.IO.Directory.Move(source, destination); 
}
// File Not found exception unhandled
Jon Skeet
people
quotationmark

The documentation is fairly clear on this:

This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory. If you try to move a directory to a directory that already exists, an IOException will occur.

You're explicitly creating the destination directory before calling Move, so you will get an IOException.

Just get rid of the CreateDirectory call. That will at least allow it to potentially work - if you're actually getting a FileNotFoundException (you don't say so, but I guess that's what the comment is meant to say) then that suggests that source can't be found... although as noted in comments on this answer, that should be raising DirectoryNotFoundException.

people

See more on this question at Stackoverflow