Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is the code i have written but it is not working. please have a look

C#
string destination = Path.Combine(
                       DataFeed.CaremarkInbound.Locations.CaremarkInboundArchive, new DirectoryInfo(
                           file.CurrentFilePath).Name);
                   Directory.Move(file.CurrentFilePath, destination);


getting the exception as

C#
Cannot create a file when that file already exists


Thanks in advance
Posted
Comments
DaveAuld 13-Jan-16 10:52am    
Have you broken out the various parts and debugged to see what you are actually passing around. Also, have you checked that the destination doesn't already exist as it suggests...
[no name] 13-Jan-16 10:53am    
So please think again about what the message "Cannot create a file when that file already exists" tries to tell you....
Sergey Alexandrovich Kryukov 13-Jan-16 11:09am    
5!—SA
[no name] 13-Jan-16 11:26am    
Thank you :)
Bruno
Richard Deeming 13-Jan-16 12:04pm    
Your title says you're moving a file, but you're calling the method to move a directory. Are you sure you didn't mean to call File.Move[^] instead?

1 solution

The error is self-explanatory? Before you do the move use File.Exists to see if the destination file exists, and if it does delete it

http://www.dotnetperls.com/file-exists[^]

http://www.dotnetperls.com/file-delete[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Jan-16 11:10am    
5ed.
—SA

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