Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, October 13, 2015 2:58 PM
I am trying to use Directory.Move to move a folder. I am getting an exception of "Could not find part of the path." This exception occurs if the destination path has more than one level of sub-folders that need to be created.
The MSDN documentation for Directory.Move states, "This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory"
The problem is that it does not create all non-existent directories in the whole destination path.
If my source folder is C:\MySource and I want to move it to a new folder called C:\Dest\MyDest, the call will throw the exception because C:\Dest does not exist. Why doesn't Directory.Move create both Dest and MyDest as part of the move operation? The documentation seems to indicate that is what it should do.
All replies (7)
Wednesday, October 14, 2015 2:12 PM âś…Answered
Although the Directory.Move method is behaving the same way as the command-line move command, the implementation still seems confusing. I can call Directory.Create with multiple levels of folders and the are all created. Seems like Directory.Move should allow the same. I guess I don't understand the caveats of not having it do that.
What you ask here is a very old question: When should my code throw exceptions, rather then try to solve an issue?
At wich point do I decide "creating the folder structure up to target dir" is outside the scope of my move-code?
All I know is that some programmer back in the DOS times decided "move will return an error, rather then try to create missing folders along the way". It was his right to decide that.
The file systems developed (FAT16, Fat32, NTFS). But the move command had to stay that way, or risk breaking existing code.
Also file systems are the only system on a computer that suffer inherently from Race Condtions. So chances are if you are running into this exception, something is most definitely not right with the file system.
FS commands are notorious for very limited scope and very agressive exception throwing. Because the opposite can cause more issues then it solves in the long run.
Tuesday, October 13, 2015 3:08 PM
Hi Ken,
You either don't have permissions to rename the folder or there's a file somewhere in that folder or its sub folders that's being held open. You cannot rename any part of a folder path if there is a file open in it.
Thanks,
Sabah Shariq
Tuesday, October 13, 2015 3:08 PM
That's not my interpretation, but granted, the documentation could be clearer.
Only the single target folder is created. Intermediate folders cannot be created using Directory.Move(). If you need them, you will have to program that in separately.
Tuesday, October 13, 2015 3:16 PM
The MSDN documentation for Directory.Move states, "This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory"
The problem is that it does not create all non-existent directories in the whole destination path.
If my source folder is C:\MySource and I want to move it to a new folder called C:\Dest\MyDest, the call will throw the exception because C:\Dest does not exist. Why doesn't Directory.Move create both Dest and MyDest as part of the move operation? The documentation seems to indicate that is what it should do.
Actually the documentation seems to indicate that it will only create one dir - the target dir itself. Creating any parent dir's to the target dir is not part of Move.
This behavior is also consistent between Directory.Move function and the move command line operation in Windows.
If you would do his operation via windows explorer, you too would create the parent before you order the move. In fact you have too.
The target dir itself not existing is a common case for move.
Any parent of the target dir not existing, is not one. The Programmer of Move choose to be carefull in such a case and rather throw an exception then do something wrong.
Simply put your target path is faulty. Correct your path or create the directory beforehand in a seperate step.
Wednesday, October 14, 2015 6:52 AM
Hi Ken,
Glad to hear you have known the reason. Could you please share the reason here? It could be better to help someone who has the same issue. Thanks for your effort and understanding.
Best regards,
Kristin
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Wednesday, October 14, 2015 9:20 AM
I am definetly not called Ken. Would remember if my first name was that short.
I also don't know the reason for certain.
I just compared the commandline move* command to Directroy.Move* and noticed the behavior is identical:
Move will only create a missing target directory (wich is a very common case for move I would think).
Any missing directories along the way will result in an error
*I asume they are either both calling the same windows API function, or the Move command calls the move commandline code. That is what I would do anyway.
Wednesday, October 14, 2015 1:23 PM
Although the Directory.Move method is behaving the same way as the command-line move command, the implementation still seems confusing. I can call Directory.Create with multiple levels of folders and the are all created. Seems like Directory.Move should allow the same. I guess I don't understand the caveats of not having it do that.