eddyuk
eddyuk

Reputation: 4190

Read already opened file

Is there a way to read binary data from a read-only file? I have an Excel worksheet, which might be opened in Excel but I want to open it for read purposes only.

I tried to do it this way:

using (FileStream fileStream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))

And I am getting

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

Is there any way to achieve that?

Upvotes: 0

Views: 884

Answers (2)

Mister Epic
Mister Epic

Reputation: 16723

Change this argument:

 FileShare.Read

to this:

 FileShare.ReadWrite

You are attempting to deny write access to the file, which is causing your issue as Excel already has it open for writing.

Upvotes: 4

Gaston Siffert
Gaston Siffert

Reputation: 372

You can't open a file who are already open. Be careful to close your file after open it. And you should verify that you didn't use the file in another software (at the same time)...

Upvotes: 0

Related Questions