2

What is the best way to read image when simultaneous requests try to access the same image file?

Basically I have created tiff viewer in asp.net. It displays thumbnails of all tiff pages and upon click loads full image below. But when I try to read big tiff file (40+ MB), some thumbnails shows up and some throw access denied error. I hope I am clear enough to explain the problem.

Parth Patel
  • 307
  • 1
  • 6
  • 19

1 Answers1

2

you should open the Stream in this way:

using (Stream s = new FileStream(fullFilePath, 
                                 FileMode.Open,
                                 FileAccess.Read,
                                 FileShare.ReadWrite))
{
  // here use the stream s
}

in this way you open for read and still other processes will be able to read the file as well and only one process, not yours, could eventually acquire the write rights.

see here as well: How do I open an already opened file with a .net StreamReader?

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Well, I only want to read file. Shouldn't I use FileShare.Read instead of FileShare.ReadWrite? Btw, I think FileStream loads whole file into memory first and then perform operation, isn't it? Is there any way to improve efficiency with read only permission? – Parth Patel Nov 22 '11 at 12:26