none
Help needed! 'The handle is invalid' when trying to read the file using StreamReader from a Network path

    Question

  •  

    Hi Everybody,

     

    Currently I am developing a software to download the data from removable media devices (such as USB, DVD and Blue-ray Disc)  automatically whenever it got inserted.

     

    I am using StreamReader and StreamWriter to copy the files from those removable devices.

     

    If the user remvoes the media, when the file is being copied, more than two times, then the third time when the application starts to read, getting an IOException "The handle is invalid".

     

    Once i got this exception, the application couldn't read that particular file anymore. I have to restart the application to read the file.

     

    How can i get out of this problem?

     

    If anyone can help me, that would be really appreciated!

     

    Thanks in Advance,

    Natarajan.

    Tuesday, March 25, 2008 5:57 PM

Answers

  • There's nothing wrong with your code. I suspect the problem is with the driver for the removable device - it isn't properly handling the removal of the device while it is in use.
    Wednesday, March 26, 2008 11:06 AM

All replies

  • Please provide a small piece of code that reproduces the problem, it's near impossible to diagnose problems with code based on description alone.

     

    Tuesday, March 25, 2008 6:54 PM
    Moderator
  • Here is the code snippet

     

    try

    {

    FileStream inStream = new FileStream(sSourceFile, FileMode.Open, FileAccess.Read);

    FileStream outStream = new FileStream(sDestinationFile, FileMode.Create, FileAccess.Write);

    byte[] buf = new byte[2048000];

    int bytesRead;

    // copy all data from in to out

    while ((bytesRead = inStream.Read(buf, 0, 2048000)) > 0)

    {

        outStream.Write(buf, 0, bytesRead);

    }

    }

    catch (FileNotFoundException ex)

    {

      // Log exception Message

    }

    catch (FileLoadException ex)

    {

       // Log exception Message

      CloseFileIOStream(ref inStream, ref outStream);

    }

    catch (DirectoryNotFoundException ex)

    {

       // Log exception Message

    }

    catch (System.UnauthorizedAccessException ex)

    {

       // Log exception Message

    }

    catch (Exception ex)

    {

      // Log exception Message

      CloseFileIOStream(ref inStream, ref outStream);

    }

    Tuesday, March 25, 2008 9:22 PM
  • It would help to have a snippet that compiles.  In your snippet you will get an error on all calls to CloseFileIOStream because inStream and outStream do not exist in that context.

     

    I would suggest the using statement to wrap around your stream objects to ensure they are disposed (which would include freeing of the handle, which may solve your problem):

    Code Snippet

    using (FileStream inStream = new FileStream(sSourceFile, FileMode.Open, FileAccess.Read))

    {

        using (FileStream outStream = new FileStream(sDestinationFile, FileMode.Create, FileAccess.Write))

        {

            byte[] buf = new byte[2048000];

     

            int bytesRead;

     

            // copy all data from in to out

     

            while ((bytesRead = inStream.Read(buf, 0, 2048000)) > 0)

            {

                outStream.Write(buf, 0, bytesRead);

            }

        }

    }

     

     

    Tuesday, March 25, 2008 9:28 PM
    Moderator
  • Thanks for making correction in the given code snippet. I will try the 'using' statement.

     

    Will 'using' statement do the these jobs -> Flush() and Close() for Stream objects ?

     

    Here is my entire code snippet (function)

     

    Code Snippet

    public void FileCopy(string sSource, string sDestination)

    {

    FileStream inStream = null;

    FileStream outStream = null;

    try

    {

    inStream = new FileStream(sSource, FileMode.Open, FileAccess.Read);

    outStream = new FileStream(sDestination, FileMode.Create, FileAccess.Write);

    byte[] buf = new byte[2048000];

    int bytesRead;

    // copy all data from in to out

    while ((bytesRead = inStream.Read(buf, 0, 2048000)) > 0)

    {

    outStream.Write(buf, 0, bytesRead);

    }

    }

    catch (FileNotFoundException ex)

    {

       // LogErr(ex.Message);

    }

    catch (FileLoadException ex)

    {

       // LogErr(ex.Message);

       CloseFileIOStream(ref inStream, ref outStream);

    }

    catch (DirectoryNotFoundException ex)

    {

       // LogErr(ex.Message);

    }

    catch (System.UnauthorizedAccessException ex) // when disk or folder is write protected

    {

       // LogErr(ex.Message);

    }

    catch (Exception ex)

    {

       // LogErr(ex.Message);

       CloseFileIOStream(ref inStream, ref outStream);

    }

    finally

    {

       CloseFileIOStream(ref inStream, ref outStream);

    }

    }

     

    private void CloseFileIOStream(ref FileStream inStream, ref FileStream outStream)

    {

    if (outStream != null)

    {

    outStream.Flush();

    outStream.Close();

    }

    if (inStream != null)

    {

    inStream.Flush();

    inStream.Close();

    }

    }

     

     

    Tuesday, March 25, 2008 10:33 PM
  • I have tried the 'using' statement. But still i am getting the same IOException "The handle is invalid"

     

    Here is the updated code snippet as per your suggestion.

     

    Code Snippet

    public void FileCopy(string sSource, string sDestination)

    {

    try

    {

    using (FileStream inStream = new FileStream(sSource, FileMode.Open, FileAccess.Read))

    {

    using (FileStream outStream = new FileStream(sDestination, FileMode.Create, FileAccess.Write))

    {

    byte[] buf = new byte[2048000];

    int bytesRead;

    // copy all data from in to out

    while ((bytesRead = inStream.Read(buf, 0, 2048000)) > 0)

    {

        outStream.Write(buf, 0, bytesRead);

    }

    }

    }

     

    }

    catch (FileNotFoundException ex)

    {

       // LogErr(ex.Message);

    }

    catch (FileLoadException ex)

    {

       // LogErr(ex.Message);

       //CloseFileIOStream(ref inStream, ref outStream);

    }

    catch (DirectoryNotFoundException ex)

    {

       // LogErr(ex.Message);

    }

    catch (System.UnauthorizedAccessException ex) // when disk or folder is write protected

    {

       // LogErr(ex.Message);

    }

    catch (Exception ex)

    {

       // LogErr(ex.Message);

       //CloseFileIOStream(ref inStream, ref outStream);

    }

    finally

    {

       //CloseFileIOStream(ref inStream, ref outStream);

    }

    }

     

     

     

    Please correct me if am wrong anywhere!

     

    Thanks in Advance!

    Tuesday, March 25, 2008 11:48 PM
  • There's nothing wrong with your code. I suspect the problem is with the driver for the removable device - it isn't properly handling the removal of the device while it is in use.
    Wednesday, March 26, 2008 11:06 AM