Click here to Skip to main content
12,951,983 members (63,639 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 4.0.

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
Updated 27-May-12 21:25pm

1 solution

Rate this: bad
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!
devendra1511 28-May-12 5:19am
I am not able to understand.
OriginalGriff 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
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 28 May 2012
Copyright © CodeProject, 1999-2017
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