Open XML SDK: 'The process cannot access the file 'x' because it is being used by another process.'
Asked Answered
R

2

5

This is my code that modifies a PowerPoint presentation, saves it as a new file, closes it, then tries to open that file.

var doc = PresentationDocument.Open(@"d:\temp.pptx", true);    
//... proccess presentation
doc.SaveAs(@"d:\temp2.pptx");
doc.Close();

var doc2 = PresentationDocument.Open(@"d:\temp2.pptx", false);
doc2.Close();

I can not understand why run-time throws an exception:

The process cannot access the file 'x' because it is being used by another process.

Ralf answered 10/4, 2018 at 8:33 Comment(3)
try saving to stream and create new file, rather than directly calling "SaveAs".Rameriz
@codetoshare, doc is object of PresentationDocument which unfortunately has not some method to stream save.Ralf
Please post a complete enough code snippet for repro purposes; see minimal reproducible example for more information. I'd test it for you, but I don't have the time to work up an entire project...Aliaalias
R
12

I noticed that doc.SaveAs() return object, which I closed just

var savedDoc = doc.SaveAs(@"d:\temp2.pptx") as PresentationDocument;

savedDoc.Close();
Ralf answered 18/4, 2018 at 12:42 Comment(0)
P
0

You have to open your presentation with a using statement. Something along the lines of:

using (var doc = PresentationDocument.Open(@"d:\temp.pptx", true))
{    
//... proccess presentation
doc.SaveAs(@"d:\temp2.pptx");
doc.Close(); //may be unnecessary
}
Perhaps answered 11/4, 2018 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.