Click here to Skip to main content
Rate this: bad
good
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 at 13-Sep-12 3:08am
   
5'ed!
ridoy at 13-Sep-12 3:53am
   
+5
manoj12.shrivastava at 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 at 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 at 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
0 OriginalGriff 404
1 Sergey Alexandrovich Kryukov 387
2 Shemeemsha RA 148
3 Abdul Samad KP 125
4 CPallini 100
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2014
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