0

I'm working on an ASP.NET MVC3 project, and part of my project is to print PDF files. Everything worked fine until this morning, when I added System.Net.Mail to send attached files. And, also worked fine after this, but when I tried to edit information, I got this error:

The process cannot access the file 'Content\StudentPdf\student54.pdf' because it is being used by another process.

I use iTextSharp to print PDFs, but it crashes at the next line of code:

using (FileStream fs = new FileStream(Server.MapPath("~/Content/StudentPdf/student" + id + ".pdf"), FileMode.Create))

How can I find out where my file is being used in my solution? My PrintPdf method is called before the SendEmail method.

2 Answers 2

2

When you are opening a file for reading, set its FileAccess and FileShare modes:

using(var readerFileStream = new FileStream(filePath, FileMode.Open, 
                                          FileAccess.Read, FileShare.ReadWrite))
{
 // ...
}

In this case, this file won't be locked for reading or writing by other processes. Also don't forget the using part, which guaranties the release of resources.

Sign up to request clarification or add additional context in comments.

1 Comment

I do how you said, but then it will be get me an error: Stream does not support writing. when I'm try to call doc.open method
0

Are you you using TFS or other versioning tool?, maybe you included the file in your project and you have locked when you checkin, check this out.

Here is a list of directios and its use in MVC:

The App_Data folder is for storing application data.

You can create a folder in App_Data as ReportPdf and not include you generate PDF files in the project to avoid blocking.

3 Comments

What do you mean about avoid blocking?
I have a rest for a hour, when returned to the project everything works fine. And after 5-7 updates problem appear again
Do you have a report generated inside the Project? Do you have it within the control of versioning?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.