无法删除vshost.exe锁定的PDF文件 [英] Unable to delete PDF file locked by vshost.exe

查看:105
本文介绍了无法删除vshost.exe锁定的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过应用程序代码创建合并的PDF文件后,我无法从桌面删除该文件。 我收到错误消息"操作无法完成,因为该文件已在vshost32.exe中打开"当我尝试从桌面删除
文件时,看到过很多类似的帖子。 我有用于创建包含在Using语句中的PDF的代码,但应用程序仍然保留在其他地方的文件句柄上?

I am unable to delete a merged PDF file from the desktop after it has been created through application code.  I receive the error message "the action can't be completed because the file is open in vshost32.exe" when I attempt to delete the file from the desktop, and have seen many similar posts.  I have the code used to create the PDF wrapped in a Using statement, but the application is still holding onto the file handle somewhere else?

这是我的代码:

using iTextSharp.text;
using iTextSharp.text.pdf;


string fileType = "File Package";
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + fileName;

List<string> evaluatorFiles = new List<string>();
List<CaseFile> caseFiles = db.caseFileRepository.GetAllByEvaluatorId(SelectedEvaluator.ID).ToList();

foreach (CaseFile cf in caseFiles)
{
    evaluatorFiles.Add(cf.caseFilePath);
}

if (MergePDFs(evaluatorFiles, filePath))
{    
	DisplayPdfView view = new DisplayPdfView(filePath, fileType);
        view.ShowDialog();
}


private bool MergePDFs(List<string> fileNames, string targetPdf)
{
    bool merged = true;
    using (FileStream fs = new FileStream(targetPdf, FileMode.Create))
    {
        Document document = new Document();
        PdfCopy pdf = new PdfCopy(document, fs);
        PdfReader reader = null;
        try
        {
            document.Open();
            foreach (string file in fileNames)
            {
                reader = new PdfReader(file);
                pdf.AddDocument(reader);
                reader.Close();
            }
        }
        catch (Exception)
        {
             merged = false;
             if (reader != null)
             {
                 reader.Close();
             }
        }
        finally
        {
            if (document != null)
            {
                document.Close();
            }                                                                               }
        }
    }

    return merged;
}           




我缺少什么? 谢谢。


What am I missing?  Thanks.

推荐答案

嗨rcmpgrc,

Hi rcmpgrc,

我猜你呢?不要处置你的一个资源。

I guess that you're not disposing one of your resources.

FileStream,Document,PdfCopy,PdfReader都实现了IDisposable。这些都需要被处理或包装在使用块中(最好是后者)。

FileStream, Document, PdfCopy, PdfReader all implement IDisposable. These all need to either be Disposed or wrapped in a using block (preferably the latter).

我的猜测是保持这些文件打开的PdfCopy实例。

My guess is it's the PdfCopy instances that are holding those files open.

您可以在所有这些中使用using语句进行清理。

You can use using statement in all these to cleanup.

最好的问候,

Cherry


这篇关于无法删除vshost.exe锁定的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆