Share via

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

Giulio Andolfi 1 Reputation point
2020-10-26T16:09:04.697+00:00

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.".
I do not know why the method InsertFromFile lock my file.
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;
            }
        }
Community Center | Not monitored
0 comments No comments

2 answers

Sort by: Most helpful
  1. Giulio Andolfi 1 Reputation point
    2020-10-27T09:03:02.33+00:00

    Hi @Emi Zhang-MSFT ,
    thanks for your information.
    I posted my question in General Office Development forum.
    Regards,
    Giulio

    Was this answer helpful?

    0 comments No comments

  2. Emi Zhang-MSFT 30,196 Reputation points Microsoft External Staff
    2020-10-27T06:50:44.98+00:00

    Hi,
    Based on your description, I suggest you post this issue to General Office Development forum:
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=officegeneral
    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.