Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,
I'm having problems with moving a directory to another location. Everytime i try to move it, it tells me the file already exists. Here is the code
Directory.Move(textBox3.Text, textBox2.Text);

textBox2 is the path for the folder i want the directory to be moved to. textBox3 is the path for the directory i want to move. All i keep getting is an IOException Cannot create a file when that file already exists. Ive searched for this problem for hours now with no luck. Thanks

Christian Graus: Yes i am aware naming conventions make it readable. Yes my textbox source and dest are correct. Even if it was not correct, it would just move the other folder into the one i want to move, however it is not working.

DaveyM69: Yes i read that link several times lol. I've tried appending the source folder to the destination path, however i seem to have problems with removing the folders entire path instead of it just using the folder name. So when i append it, it gives me an error "The given path's format is not supported". Do you know how i could remove the whole path, so i just have the Pakoo name that i can append and attempt?

And thanks to both of you for helping me with this.


DaveyM69: Thank you. Works perfectly. Thanks again
Posted
Updated 28-Mar-10 13:40pm
v4

I suspect you're not appending the final directory name to your destination path.

Look at the remarks section on this[^] page.

Using your example, textBox2.Text would need to be
C:\Users\ME\Downloads\Pakoo
not
C:\Users\ME\Downloads
as C:\Users\ME\Downloads already exists.
 
Share this answer
 
v3
I've just tested this code and it seems to do what you want, just change the hardcoded values for the ones from your Textboxes and add error checking/exception catching.
C#
//Moves the 'Test' folder
string source = @"C:\Users\Dave\Documents\Test";
string destination = Path.Combine(
    @"C:\Users\Dave", new DirectoryInfo(
        source).Name);
Directory.Move(source, destination);
 
Share this answer
 
v2
Well, are you certain you have the parameters in the right order ? Absolutely SURE the directory does not exist ?
 
Share this answer
 
Please don't push 'answer' to reply, unless you're posting an answer. Edit your post.

And textbox3 is where you enter the source, and textbox2 is where you enter the destination ? Did it occur to you that having meaningful names like txtSource and txtDest would make your code readable ? You've stepped through the debugger and the directories are definitely source, destination, not the opposite ?
 
Share this answer
 
Yes i am sure the directory is not there. I cut the directory from the area i want it to be and put it on the desktop. Heres a better example

I have the directory name Pakoo. The origional it was located at:
C:\Users\ME\Documents\Pakoo


And i want to try to move it to the downloads folder, so in the end it will be:
C:\Users\ME\Downloads\Pakoo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900