该进程无法访问文件"...",因为它正在被另一个进程使用 [英] The process cannot access the file '...' because it is being used by another process

查看:233
本文介绍了该进程无法访问文件"...",因为它正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code to delete file in my folder, but in my line code, I want to delete two file together with different folder. But I always get an error "the process cannot access.... another process". May be you can correct my code and give me a solution. Thanks

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

1) I have a code to generate watermark when save file(.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)当从一页中一起删除详细数据时,我会调用此代码.

2) And this code i call when delete detail data from one page together.

    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);
            } 
        }
    }

希望您能理解我的意思.

I hope you understand what I mean.

谢谢.

推荐答案

我的间谍感告诉我您需要致电

My spidey senses tells me you need to call

reader1.Close();

这篇关于该进程无法访问文件"...",因为它正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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