Directory.move

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I do not understand how this function should work. It seems to be different than when I drag a folder ...

error message: Cannot create a file when that file already exists.

c:\atest has subfolder btest which has subfolder ctest plus a single file.
( c:\atest\btest\ctest\test.txt )
c:\aatest is an empty folder.

If I drag atest to aatest, I will have c:\aatest\atest\btest\ctest\test.txt
But as noted using the function as below wont work.
So conceptually I am mixed up, how should I think about this function, Thanks !!!

try
{
string sorDir=@"c:\atest";
string desDir=@"c:\aatest";
if ((Directory.Exists(sorDir)==true) && (Directory.Exists(desDir)==true))
{
Directory.Move(sorDir,desDir);
}
}
catch ( Exception e )
{
Console.WriteLine("What now : " + e.Message);
}
 
string desDir=@"c:\aatest";
should be
string desDir=@"c:\aatest\atest";
Please read the documentation on Directory.Move, especially the remark
paragraph.

Willy.
 
Hi andrewcw,

Thank you for posting in the community! My name is Jeffrey, and I will be
assisting you on this issue.

Based on my understanding, when you use Directory.Move to move a directory
into an emtpy one, an error message: "Cannot create a file when that file
already exists. " generated.

========================================================
In Directory.move docuement, you will find that:
"This method throws an IOException if, for example, you try to move
c:\mydir to c:\public, and c:\public already exists. You must specify
"c:\\public\\mydir" as the destDirName parameter, or specify a new
directory name such as "c:\\newdir"."

So you should specify your destination directory's name in that empty
directory, for your situation, you should use:
string desDir=@"c:\aatest\atest";

========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi andrewcw,

Is your problem resovled?
If you still have any concern, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top