locked
System.IO.File.ReadAllText() throws "The handle is invalid" error RRS feed

  • Question

  • What could be the reason why ReadAllText() throws an error saying "The handle is invalid."?
    There's nothing special with my code:


    string fileName = Path.Combine("\\server\Scripts"  , "DB.sql");
    string script = File.ReadAllText(fileName);


    This used to work. Now I get the error.
    Here is the stack trace:


    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
    at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
    at System.IO.StreamReader..ctor(String path, Encoding encoding)
    at System.IO.File.ReadAllText(String path, Encoding encoding)

    Wednesday, June 3, 2009 2:35 AM

Answers

  • Hi nobugz!

    Thanks for the reply! We've figured out already what's causing the problem.
    The remote computer, where the file being read is located, has been rebooted;
    and some necessary services in that computer did not start after the reboot.
    The reboot also caused file sharing to be turned off.
    These have made the remote computer inaccessible to the machine where the File.ReadAllText() method has been called.

    Thursday, June 4, 2009 3:23 AM

All replies

  • This is *very* unusual.  I can only imagine that some kind of misbehaving DLL got injected into your program and has a bug, closing the wrong file handle.  A virus scanner could be the source of that.  That should be intermittent.  If this consistently fails, there is something very wrong with the machine.

    Hans Passant.
    Wednesday, June 3, 2009 9:21 AM
  • Hi nobugz!

    Thanks for the reply! We've figured out already what's causing the problem.
    The remote computer, where the file being read is located, has been rebooted;
    and some necessary services in that computer did not start after the reboot.
    The reboot also caused file sharing to be turned off.
    These have made the remote computer inaccessible to the machine where the File.ReadAllText() method has been called.

    Thursday, June 4, 2009 3:23 AM