2013-06-04 1 views
0

Dans mon projet, j'accède à un fichier local et le copie dans un autre fichier, puis le tatoue, mais pendant la copie, j'obtiens l'exception IO « Impossible d'accéder au fichier » Je suis sûr que le fichier est libre et non accessible par tout autre procédé tout organisme peut me dire quel serait le problème estProcessus Impossible d'accéder au fichier " Path" car il est utilisé par un autre processus

Mon code est

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_Kumar\Application\Valuation\ExamManagement\ExamManagement\FileUpload\"+file; 
      string filename1 [email protected]"E:\Rajesh_Kumar\Application\Valuation\ExamManagement\ExamManagement\FileUpload\" + file; ; 
      bool b = true;// File_lock(filename); 
      if(b==true) 
      { 
       File.Copy(Path.Combine(@"E:\Rajesh_Kumar\Application\Valuation\ExamManagement\ExamManagement\FileUpload", 
       filename), Path.Combine(@"E:\Rajesh_Kumar\Application\Valuation\ExamManagement\ExamManagement\UFileUpload ", 
       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); 
     }   
    }. 
+3

vos entrées et les fichiers de sortie sont les mêmes. – Igarioshka

+0

@lgarioshka Si j'essaie avec des noms de fichiers différents, la même exception se produit, quand je commente la ligne File.Copy et exécute le programme, il fonctionne correctement en ajoutant un filigrane mais sans créer de nouveau fichier – Rajesh

+1

Et si vous passez un autre nom de fichier au document .Enregistrer (nom de fichier); afin qu'il enregistre le document traité à un autre chemin? – Igarioshka

Répondre

2

Le problème est que vous essayez de copier le fichier lui-même et essayer de remplacer le fichier dans le processus

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 
+0

@ lagarioshka problème que je l'avais résolu en renommant 'chaîne nomfichier1 = @" E: \ Rajesh_Kumar \ Application \ Valuation \ ExamManagement \ ExamManagement \ FichierUpload \ Nouveau_ "+ fichier;' – Rajesh

-1

I y a déjà été, peut-être que vous pourriez essayer de le faire:

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; 
} 
Questions connexes