进程无法访问文件错误 - iTextSharp

时间:2015-03-24 23:02:24

标签: c# asp.net file-io itextsharp

我使用iTextSharp库创建pdf以下功能。

    public static string SavePDF(int id, bool isApproved)
    {
        string path = "";
        using (Document doc = new Document(PageSize.A4, 42, 53, 70, 46))
        {
            PdfHelper page = new PdfHelper();
            path = System.Web.HttpContext.Current.Server.MapPath("~/Files/File_" + id + ".pdf");

            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (PdfWriter pdfWriter = PdfWriter.GetInstance(doc, fs))
                {
                    pdfWriter.PageEvent = page;

                    doc.Open();
                    //code to add contents to pdf

                    doc.Close();
                    fs.Close(); 
                } 
            } fs.Dispose();
        }
        System.Threading.Thread.Sleep(1000);
        return path;
    }

在.NET应用程序中调用上述函数两次。但是在第二次调用时,它给出了以下错误:

System.IO.IOException: The process cannot access the file 'C:\inetpub\wwwroot\XXXXXWebsite\Files\File_1605.pdf' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode)

我对这个错误感到非常困惑,因为我通过使用语句以及Close()和Dispose()对象多次更改了代码。甚至使用Threading等待操作首先完成,但我似乎得到了同样的错误。

任何帮助都将不胜感激。

0 个答案:

没有答案