【问题标题】:The process cannot access the file '...' because it is being used by another process该进程无法访问文件“...”,因为它正被另一个进程使用
【发布时间】:2018-04-03 07:00:40
【问题描述】:

我有一个删除文件夹中文件的代码,但在我的行代码中,我想删除两个文件以及不同的文件夹。但我总是收到一个错误“进程无法访问....另一个进程”。也许您可以更正我的代码并给我一个解决方案。谢谢

1)我有一个代码可以在保存文件(.pdf)时生成水印:

public bool InsertWaterMark(string path)
    {
        bool valid = true; 
        string FileDestination = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path;
        string FileOriginal = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path.Replace("FileTemporary", "FileOriginal");
        System.IO.File.Copy(FileDestination, FileOriginal); 
        string watermarkText = "Controlled Copy";

        #region process
        PdfReader reader1 = new PdfReader(FileOriginal);//startFile
        using (FileStream fs = new FileStream(FileDestination, FileMode.Create, FileAccess.Write, FileShare.None))//watermarkedFile
        {
            using (PdfStamper stamper = new PdfStamper(reader1, fs))
            {
                int pageCount1 = reader1.NumberOfPages; 
                PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                for (int i = 1; i <= pageCount1; i++)
                {
                    iTextSharp.text.Rectangle rect = reader1.GetPageSize(i); 
                    PdfContentByte cb = stamper.GetUnderContent(i); 
                    cb.BeginLayer(layer);
                    cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 80);
                    PdfGState gState = new PdfGState();
                    gState.FillOpacity = 0.15f;
                    cb.SetGState(gState);
                    cb.SetColorFill(BaseColor.GRAY);
                    cb.BeginText();
                    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
                    cb.EndText(); 

                    PdfContentByte canvas = stamper.GetUnderContent(i);
                    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    canvas.SetColorFill(BaseColor.RED);
                    PdfGState gStateFooter = new PdfGState();
                    gStateFooter.FillOpacity = 1f;
                    canvas.SetGState(gStateFooter);
                    canvas.BeginText();
                    canvas.SetFontAndSize(bf, 12);
                    canvas.ShowTextAligned(PdfContentByte.ALIGN_CENTER, '"' + "When printed, this documents are considered uncontrolled" + '"', 300.7f, 10.7f, 0);
                    canvas.EndText();
                    cb.EndLayer();
                }
            }
        }
        #endregion
       return valid;
    }

2)当从一页中删除详细数据时,我会调用此代码。

    public ActionResult Delete(string parm)
    {
        TableEDIS data = db.TableEDISs.FirstOrDefault(e => e.detail_guid_edis == new Guid(parm));
        string fisikFile = data.upload_document;
        string fisikFileFormulir = data.upload_document_formulir;

        if (!string.IsNullOrEmpty(fisikFile))
        {
            var relativePath = "~/Content/" + fisikFile;
            var absolutePath = HttpContext.Server.MapPath(relativePath);
            var absolutePathOriginal = HttpContext.Server.MapPath(relativePath.Replace("Temporary", "Original"));

            if (Directory.Exists(Path.GetDirectoryName(absolutePath)))
            {
                System.IO.File.Delete(absolutePath);
            } 

            if (Directory.Exists(Path.GetDirectoryName(absolutePathOriginal)))
            {
                System.IO.File.Delete(absolutePathOriginal);
            } 
        }
    }

我希望你明白我的意思。

提前致谢。

【问题讨论】:

  • 这与尝试以一种方法删除两个文件无关,您只是尝试删除某个应用程序正在使用的文件。您在软件中打开文件了吗?
  • 在这种情况下,猜测 File.ExistsDirectory.Exists 更好。
  • 这是您会遇到的最常见的错误消息之一。以我的经验,“其他过程”实际上更有可能是您自己的过程。检查您是否没有打开/泄漏与这些文件相关的任何其他对象,例如流。
  • 在删除文件的命令之前。在我的控制器文件中还有其他过程,例如使用 itextsharp、pdfreader 为 pdf 创建水印。它有影响吗?如果是,现在我该怎么办?
  • 您没有碰巧在某处打开了 pdf 文件? Adobe reader 会锁定其打开的 pdf 文件。

标签: c# system.io.file


【解决方案1】:

我的感觉告诉我你需要打电话

reader1.Close();

【讨论】:

  • 它有效,天哪,我忘记关闭它了。呵呵……非常感谢
  • 我在使用 Spring Batch 的多线程环境中工作,遇到了这个错误,并认为这意味着我将在接下来的一周里试图解决它,但不,你的蜘蛛侠感觉今天救了我,希望我能投票 100 次
  • 很高兴我能帮上忙
猜你喜欢
  • 2010-12-10
相关资源
最近更新 更多