进程无法访问文件"\Path",因为它正被其他进程使用



在我的项目中,我正在访问一个文件本地文件并将其复制到另一个文件,然后对其进行水印,但在复制时,我得到IO异常"无法访问文件",我确信该文件是自由的,没有被任何其他进程访问,任何人都可以告诉我问题是什么

我的代码是,

  protected void AddWaterMark(string file)
    {
        string watermark = "Confidential Document Printed on " + DateTime.Now.ToString();
        const int emSize = 40;
        try
        {
            // Get a fresh copy of the sample PDF file
            string filename = @"E:Rajesh_KumarApplicationValuationExamManagementExamManagementFileUpload"+file;
            string filename1 =@"E:Rajesh_KumarApplicationValuationExamManagementExamManagementFileUpload" + file; ;
            bool b = true;// File_lock(filename);
            if(b==true)
            {
                File.Copy(Path.Combine(@"E:Rajesh_KumarApplicationValuationExamManagementExamManagementFileUpload",
                filename), Path.Combine(@"E:Rajesh_KumarApplicationValuationExamManagementExamManagementUFileUpload ",
                filename1), true); //Exception was Thrown Here
            // Create the font for drawing the watermark
            XFont font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);
            // Open an existing document for editing and loop through its pages
            PdfDocument document = PdfReader.Open(filename);
            // Set version to PDF 1.4 (Acrobat 5) because we use transparency.
            if (document.Version < 14)
                document.Version = 14;
            for (int idx = 0; idx < document.Pages.Count; idx++)
            {
                //if (idx == 1) break;
                PdfPage page = document.Pages[idx];
                // Variation 1: Draw watermark as text string
                // Get an XGraphics object for drawing beneath the existing content
                XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
                // Get the size (in point) of the text
                XSize size = gfx.MeasureString(watermark, font);
                // Define a rotation transformation at the center of the page
                gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
                gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
                // Create a string format
                XStringFormat format = new XStringFormat();
                format.Alignment = XStringAlignment.Near;
                format.LineAlignment = XLineAlignment.Near;
                // Create a dimmed red brush
                XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
                // Draw the string
                gfx.DrawString(watermark, font, brush,
                  new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),format);
            }
            // Save the document...
            document.Save(filename);
            // ...and start a viewer
            Process.Start(filename);
            File.Exists(filename);
            }
        }
        catch(Exception ex)
        {
            ClientMessaging(ex.Message);
        }           
    }.

问题是您试图将文件复制到自身并试图在进程中覆盖该文件

File.Copy("TextFile1.txt", "TextFile1.txt", true); //throws the error: "The process cannot access the file 'TextFile1.txt' because it is being used by another process."
File.Copy("TextFile1.txt", "TextFile2.txt", true); //copies the file

我以前也遇到过这种情况,也许你可以试着这样做:

public bool FileIsLocked(string strFullFileName)
{
    bool blnReturn = false;
    System.IO.FileStream fs = null;
    try {
        fs = System.IO.File.Open(strFullFileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, System.IO.FileShare.None);
        fs.Close();
    } catch (System.IO.IOException ex) {
        blnReturn = true;
    }
    return blnReturn;
}

最新更新