1

I've never came across this issue. Can anybody tell me why does this code just hang and hang forever:

 FileStream stream = File.Open("abc.xls", FileMode.Open, FileAccess.Read);

I am using Visual Studio 2010 Premium edition, Windows 7.

What can be the possible causes? Either it should throw exception or just run fine. Why does the program just hang and never reach the next line of code after this?

Thanks in advance :)

Jaggu
  • 6,298
  • 16
  • 58
  • 96

2 Answers2

1

A possible cause could be the size of the file, how big is it?

saj
  • 4,626
  • 2
  • 26
  • 25
  • No silly me. It appears the file was open. So the program just hanged. As soon as I close the file it ran perfectly. But why does the program have problem when reading if it is already open? – Jaggu Jul 14 '11 at 08:33
  • Because it can not obtain access to the file. The file is being used by another process – saj Jul 14 '11 at 08:35
  • 1
    @Bob, it's most likely because the file is locked. First check if the file is locked.! http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c – Jethro Jul 14 '11 at 08:36
  • @Jethro : Exactly what I faced. – Jaggu Jul 14 '11 at 09:02
1

Can this same file be opened fine with other applications? Is it on a network or any other special kind of drive which may cause a delay? Are you sure that it is this one line which blocks (the debugger is sometimes a line off)?

Note: In contrast to the other answer given, I believe that the size should not have any impact on the performance when opening a stream. It doesn't actually read or do anything but opening the file.

Lucero
  • 59,176
  • 9
  • 122
  • 152