Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am copying my files to temp folder for quick access. For that i am using this code.

C#
if (File.Exists(tempAudpath))
{
    File.Delete(tempAudpath);
    File.Copy(Audpath, tempAudpath, true);
}
else
{
    File.Copy(Audpath, tempAudpath, true);
}

But delete is not happening. It is showing that another process is using.
Can any one help me to delete and copy files in this scenario.

[edit]"Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 13-Jul-11 21:14pm
v3
Comments
Sergey Alexandrovich Kryukov 14-Jul-11 2:52am    
Do you have the code with you use the file to be deleted? Read, write, whatever...
--SA

1 solution

Since the exception is showing "File in use by another process", it is not that you are trying to delete a directory - which File.Delete can't do. That was my initial thought when I saw your file names.

Without seeing the rest of your code - and I don't want you to post it all - I can't be too specific, but...

Since the name implies that this is a temporary file, I can only assume that your application has just written it? In which case, it is not quite as simple a fix as you think - the problem is not in this code fragment.
What has happened is that your code to write the file is not clean enough - you have not disposes all the objects which access the file. Since one or more objects have not been disposed, they still have what are called File Handles attached to the file. Since these exist, the system assumes (quite rightly) that the file is in use, and will not let you delete it.

Look at the other code which uses your temporary file, and Dispose (or preferably surround with a using block all the objects (streams, binaryWriters, textWriters, etc.) which access it.
 
Share this answer
 
Comments
[no name] 14-Jul-11 3:15am    
Excellent. +5.
Naseer Ahamed M 14-Jul-11 5:12am    
Hi Koushik,
Thanks for your reply. it is not written by the application. i have written that. i have checked that dispose thing also. can please tell me any other way to do that.
OriginalGriff 14-Jul-11 5:17am    
Show us the code fragment which writes the file, with all the Dispose method calls or using blocks.

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