l15a
l15a

Reputation: 2617

Excel Interop: how to lock file on SaveAs

Does Excel.Workbook.SaveAs() put a lock on the file? If not, how would I force a lock on the file while it's saving?

I'm writing a Windows Service that runs on multiple machines. The Windows Service writes Excel files to a shared mount. I want to make sure that the Excel file is locked while saving so another process is not able to write to it until the file is closed by the first process.

Upvotes: 1

Views: 704

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172468

When you will save the file the file will be locked so no one has the access to open it. It will be locked. Are you looking for something like this:

workbook.SaveAs(filename, AccessMode: XlSaveAsAccessMode.xlExclusive);

You may also check XlSaveAsAccessMode Enumeration

Specifies the access mode for the Save As function.

Upvotes: 1

Related Questions