none
Presentation.Slides.InsertFromFile locks my files and I can not delete it RRS feed

  • Question

  • Hello everyone,
    I have a problem using Presentation.Slides.InsertFromFile method because after this istruction my file can not be deleted.
    The system returns the error "The process cannot access the file 'C:\\temp\\pptFiles\\internal\\Copy_InternalFileName.pptx' because it is being used by another process.".

    The error occurs only on Office (365) 16.0.13001.0000 or later.

    The error doesn' t occur on Office (365) 16.0.11001.20038 or on Office 2016.

    Could you have any idea how can I unlock and delete the file?
    Thanks,
    Giulio


    This is my code:

    public void UpdateDocument(DocumentWrapper wrapper)
             {
                 string internalFileName = @"C:\temp\pptFiles\internal\InternalFileName.pptx";
        
                 string internalCopyFileName = internalFileName.Replace("Internal", "Copy_Internal");
        
                 FileInfo fileInfo = new FileInfo(internalCopyFileName);
                 if (fileInfo.Exists)
                 {
                     fileInfo.Delete();
                 }
        
                 File.Copy(internalFileName, internalCopyFileName);
        
                 Globals.ThisAddIn.Application.Presentations.Open(internalCopyFileName,
                     MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
                 globalPresentation = (Presentation) wrapper.presentation;
                 globalPresentation.Slides.InsertFromFile(internalCopyFileName, 0, 1, 1);
                 globalPresentation.Save();
        
                 try
                 {
                     System.IO.File.Delete(internalCopyFileName);
                 }
                 catch (Exception exception)
                 {
                     Console.WriteLine(exception);
                     throw;
                 }
             }


    Tuesday, October 27, 2020 9:00 AM