File.Create和File.OpenWrite即使已关闭也被处置也不会释放文件

问题描述 投票:-1回答:2

我正在使用HttpWebRequest对象下载pdf文件,并使用所有“使用”块以及复制后的.Close方法将响应中的内容直接从响应流直接写入FileStream中。下一步,我需要使用一些第三方库(iText7)从该pdf文件中提取一些文本,但是它无法访问该文件。起初,我认为这是与iText7相关的问题,但后来我意识到似乎并非如此,因为我什至无法从文件资源管理器中删除文件,而我自己的应用程序出现“文件正在使用”错误。

这是示例代码:

            HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
            webReq.AllowAutoRedirect = true;
            webReq.CookieContainer = Cookies;
            webReq.UserAgent = UserAgent;
            webReq.Referer = Referrer;
            webReq.Method = WebRequestMethods.Http.Get;

            using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
            {
                using (Stream httpResponseStream = response.GetResponseStream())
                {
                    using (FileStream output = File.Create(file1))
                    {
                        httpResponseStream.CopyTo(output);
                        output.Close();
                    }
                    httpResponseStream.Close();
                    response.Close();

                    Cookies = webReq.CookieContainer;
                }
            }

            GC.Collect();

            ExtractPDFDoc(file1);//error throws in this function and the exception.message is "Cannot open document."

            Console.WriteLine("now waiting to let you check the file is in use? try delete it manually...");
            Console.ReadKey(); //added this line to ensure that file is actually in use. I can't even delete the file manually from windows file explorer at this time. But, interestingly, Acrobat Reader can OPEN the file when I double click, which makes me thing that Adobe and iText7 uses different methods to open the pdf file - but anyway - I can't help it tho.

您能帮忙这里有什么问题吗?

对于那些想要查看ExtractPDFDoc()方法的人:

public static object ExtractPDFDoc(string filename)
    {

        iText.Kernel.Pdf.PdfReader pdfReader = null;
        iText.Kernel.Pdf.PdfDocument pdfDocument = null;


        try
        {

            pdfReader = new iText.Kernel.Pdf.PdfReader(filename);

            pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader);

        }
        catch (Exception ex)
        {

            pdfReader = null;
            pdfDocument = null;

            return new Exception(string.Format("ExtractPDFDoc() failed on file '{0}' with message '{1}'", filename, ex.Message));

            //this is where I get the error, ex.Message is 'Cannot open document.'
            //however, I can open it in Adobe Reader but I can't delete it before closing my app.
        }
}
c# file stream
2个回答
1
投票
public static object ExtractPDFDoc(string filename) { iText.Kernel.Pdf.PdfReader pdfReader = null; iText.Kernel.Pdf.PdfDocument pdfDocument = null; try { pdfReader = new iText.Kernel.Pdf.PdfReader(filename); pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader); } catch (Exception ex) { throw new Exception(string.Format("ExtractPDFDoc() failed on file '{0}' with message '{1}'", filename, ex.Message), ex); } finally { pdfReader?.Dispose(); pdfDocument?.Dispose(); } }

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.