Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with deleting the file in C#
In my code

C#
if (System.IO.File.Exists(destination))
{
    System.IO.File.Delete(file);
}
System.IO.File.Copy(source,destination);
System.IO.File.Delete(source);

In my code the problem is
destination file already exist when i create a new file it will copied into destination folder with the name as destination

problem occurred when file size is big..

problem is file delete is not completed and file copy is start......
:(....

sorry for my english....


Any Help will sufficient....

Thanks..
Posted

Just use File.Move:
C#
if (System.IO.File.Exists(destination))
{
    System.IO.File.Delete(file);
}
System.IO.File.Move(source,destination);
 
Share this answer
 
Comments
_Amy 13-Sep-12 3:08am    
5'ed!
ridoy 13-Sep-12 3:53am    
+5
manoj12.shrivastava 13-Sep-12 5:32am    
sir i have tried this but it is not working for me....
i m getting error can not create file it is already exist....
OriginalGriff 13-Sep-12 5:35am    
Ah.
It's pretty obvious what the problem is.
Try changing "file" to "destination"
System.IO.File.Delete(file);
Becomes
System.IO.File.Delete(destination);
manoj12.shrivastava 13-Sep-12 5:48am    
Sir,
Thanks for you reply ....
But in my case it's not working ..
actually the problem is

I have to delete file first if exist in destination folder
after that copy/move new file with the same name in destination folder..

My problem is that the file being deleted is a large video file typically 60 to 80MB, so this takes time to delete. In the mean time the following file copying code executes and tries to replace the file. Is there a way for me to hold the file copy operation and until the deletion is completed?

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