Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Aspnet
ForumUnansweredBonus QuestionsArticlesFAQsSearchContestMy AccountSign-out The process cannot access the file because it is being used by anotherAsked By dev rathore on 28-May-12 02:14 AMHi All,
 
I have used below code to delete temp file(.PDF) in ASP.net 4.0.
if(File.Exists(FilePath))
{
try
{
File.Delete(FilePath);
}
 

but gettting below error:
The process cannot access the file 'D:\devendra\web\TempWorkAreaPdf\AdmitCard_C58YE26SB7.pdf' because it is being used by another process.

Please help me so that i can delete the tempfile.
Posted 27-May-12 21:09pm
Edited 27-May-12 21:25pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The chances are it is your code that is using the file: check where you create and use the temporary file, and ensure that you have correctly Closed and Disposed all file streams and related objects when you have finished with it. Just letting them go out of scope does not do this - it waits for the Garbage collector to do it which may not be for weeks!
  Permalink  
Comments
devendra1511 at 28-May-12 5:19am
   
I am not able to understand.
OriginalGriff at 28-May-12 5:24am
   
Since this is a temporary file, it is your code that creates it, yes?
Look at your code. Wherever and however you create the .PDF file, you need to make sure that all Stream or other File related objects have the Close() and Dispose() methods called on them, either directly or via a using block when you have finished creating them.
If you don't, then the file will be open for writing until the garbage collector decides they are no longer referenced and does it for you, or your application closes. Since the garbage collector will not do anything until you start to run low on memory, it is very likely that your code will still have the file open until your app exits.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 231
3 Sergey Alexandrovich Kryukov 211
4 kbrandwijk 204
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 405
4 thatraja 370


Advertise | Privacy | Mobile
Web03 | 2.8.140902.1 | Last Updated 28 May 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