0

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?

Karl-Henrik
  • 1,113
  • 1
  • 11
  • 17
eddyuk
  • 4,110
  • 5
  • 37
  • 65
  • possible duplicate of [Exception in opening a file that is already open](http://stackoverflow.com/questions/3846646/exception-in-opening-a-file-that-is-already-open) – Robert H Apr 15 '14 at 14:13

2 Answers2

4

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.

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
0

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)...

Gaston Siffert
  • 372
  • 1
  • 8