Last post Mar 31, 2017 02:47 PM by dlchase
Mar 30, 2017 10:50 PM|dlchase|LINK
I am trying to move the folder and files from one directory to another. The code below is giving me the error "Cannot create a file when that file already
The source directory is \\server\photos\Rodriguez, Michael\RO42128 and has 15 files
The destination directory is \\server\share\Rodriguez, Melly
Mar 31, 2017 02:55 AM|Eric Du|LINK
According to your description, as far as I know, you are creating the Directory if it doesn't exist and then trying to move your directory, the Move Method will create the directory.
If the Directory already exists you will need to Delete it or Move it. Please refer to the following code:
protected void Button1_Click(object sender, EventArgs e)
string sourcedirectory = @"C:\source";
string destinationdirectory = @"C:\destination";
string backupdirectory = @"C:\Backup";
Directory.Move(destinationdirectory, backupdirectory + DateTime.Now.ToString("_MMMdd_yyyy_HHmmss"));
Mar 31, 2017 03:03 AM|kaushalparik27|LINK
The issue occurred because Rodriguez directory already exists at \\server\share. The destination Directory cannot exist. The Move Method will create directory first in order to move. I would suggest check for Directory.Exists() on destination and it exists
then delete or take backup (whatever suits based on your requirement).
Mar 31, 2017 03:15 AM|KathyW|LINK
It is telling you that \\server\share\Rodriguez already exists. You can't move a directory to a directory that already exists. (https://msdn.microsoft.com/en-us/library/system.io.directory.move%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396)
If you are trying to move just the files, you would iterate through the files in the source directory and File.Move them into the target, existing directory.
Mar 31, 2017 01:16 PM|dlchase|LINK
I DO want it to create a directory named RO42128 into
\\server\share\Rodriguez, Melly\ folder and then bring all of it's files and subdirectories along.
The folder named \\server\share\Rodriguez, Melly\RO42128 does not currently exist.
Mar 31, 2017 01:21 PM|kaushalparik27|LINK
Then your destination path should be \\server\share\Rodriguez\Melly\RO42128 and not just
Mar 31, 2017 02:02 PM|dlchase|LINK
I must be getting closer because it now creates the new directory named ...Rodriquez, Melly\RO42128 but then fails with the same error which doesn't make sense because the new folder is empty. Is my only option to iterate through the files in old directory
and move them 1 by 1 to the new directory? I thought that was what the Directory.Move method did.
Mar 31, 2017 02:24 PM|dlchase|LINK
I ended up changing to the Directory.MoveTo method and that did the trick.
Mar 31, 2017 02:26 PM|kaushalparik27|LINK
I believe you are trying to actually merge documents/folders at your destination folder. If that is the case, I do not see if Directory.Move will really help you. You may need to iterate and go file/folder by file/folder in a recursive loop.
Mar 31, 2017 02:47 PM|dlchase|LINK
Thank you. As I mentioned, the Directory.MoveTo method did what I needed and both moved the subdirectory and moved all files to the new destination folder.