<p>My web application generates pdf files and either e-mails or faxes them to our customers. Somehow IIS6 is keeping hold of the file and blocking any other requests for it claiming the old '..the process cannot access the file 'xxx.pdf' because it is being used by another process.'</p>

<p>When I recycle the application pool all is ok. Does anybody know why this is happening and how can I stop it.</p>

<p>Thanks</p>

[{'answer_id': 72007, 'author': 'Chris Roberts', 'author_id': 475, 'author_profile': 'https://Stackoverflow.com/users/475', 'pm_score': 0, 'selected': False, 'text': "<p>I'd look through your code and make sure all handles to open (generated) files have been closed properly.  Sometimes you just can't rely on the garbage collector to sort these things out.</p>\n"}, {'answer_id': 72026, 'author': 'Adam Vigh', 'author_id': 1613872, 'author_profile': 'https://Stackoverflow.com/users/1613872', 'pm_score': 1, 'selected': False, 'text': "<p>Sounds like, the files - after being created - are still locked by the worker process. Make sure that you close all the connections for your file.\n(remember, using using blocks'll take care of that)</p>\n"}, {'answer_id': 72093, 'author': 'massimogentilini', 'author_id': 11673, 'author_profile': 'https://Stackoverflow.com/users/11673', 'pm_score': 0, 'selected': False, 'text': '<p>Check that all the code writing files on disk properly close every handle using the proper .Close() in the finally clause or trough the "using" clause of C#</p>\n\n<pre><code>byte[] asciiBytes = getPdf(...);\ntry{\nBinaryWriter bw = new BinaryWriter(File.Create(filename));\nbw.Write(pdfBytes);\n}\nfinally {\n  if(null != bw)\n    bw.Close();\n}\n</code></pre>\n\n<p>Use the Response and the Content-Disposition clause to send the file</p>\n\n<pre><code>Response.ContentType = "application/pdf";\nResponse.AppendHeader("Content-disposition", "attachment; filename=" + PDID + ".pdf");\nResponse.WriteFile(filename);\nResponse.Flush();\n</code></pre>\n\n<p>The code shown creates and send Pdf files to customer from about 18 months and we\'ve never seen a file locked.</p>\n'}, {'answer_id': 72212, 'author': 'EricSch', 'author_id': 12192, 'author_profile': 'https://Stackoverflow.com/users/12192', 'pm_score': 0, 'selected': False, 'text': '<ol>\n<li><p>Like mentioned before: Take care that you close all open handlers.</p></li>\n<li><p>Sometimes the indexing service of Microsoft blocks files. Exclude your directory</p></li>\n</ol>\n'}, {'answer_id': 72268, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 4, 'selected': True, 'text': "<p>As with everyone said, do call the <code>Close</code> and <code>Dispose</code> method on any IO objects you have open when reading/writing the PDF files.</p>\n\n<p>But I suppose you'd incorporated a <strong>3rd party component?</strong> to do the PDF writing for you? If that's the case you might want to check with the vendor and/or its documentation to make sure that you are doing things in the way the vendors intended them to be. Don't trust the black box you got from someone else unless it has proven itself.</p>\n\n<p>Another place to look might be what happens during multiple web request to the PDF files, are you sure that the file is not written simultaneously from multiple places? e.g. 2-3 requests genrating PDF simultaneously? or 2-3 pages along the PDF generation process?</p>\n\n<p>And lastly, you might want to check the exception logs to make sure that nothing is crashing/thread exiting and leaving the file handle open without you noticing it. It happens a lot in multiple threading scenarios, sometimes the thread just crashes and exits - which could happen especially if you use 3rd party components, they might be performing some <em>magic</em> tricks, you'd never know.</p>\n"}]
['https://Stackoverflow.com/questions/71971', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11707/']