Click here to Skip to main content
12,450,855 members (47,296 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I have a problem with deleting the file in C#
In my code

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......
Frown | :( ...

sorry for my english....


Any Help will sufficient....

Thanks..
Posted 12-Sep-12 20:58pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Just use File.Move:
if (System.IO.File.Exists(destination))
{
    System.IO.File.Delete(file);
}
System.IO.File.Move(source,destination);
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100