Adam Shakhabov
Adam Shakhabov

Reputation: 1346

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

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.

Upvotes: 5

Views: 2981

Answers (2)

Adam Shakhabov
Adam Shakhabov

Reputation: 1346

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

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

savedDoc.Close();

Upvotes: 12

george warner
george warner

Reputation: 11

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
}

Upvotes: 0

Related Questions