Is this a bug in .net file io? is there a work around?

From: Daniel (softwareengineer98037_at_yahoo.com)
Date: 09/28/05

  • Next message: Daniel: "is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?"
    Date: Wed, 28 Sep 2005 12:31:13 -0700
    
    

    Is this a bug in .net file io? is there a work around?

    when writing a large file with using(StreamWriter ... if the network drive
    of the share is removed then the file is never closed. I tried doing the
    using clause, i tried both close and closehandle in the finaly clause. in
    all cases if a large file is being written to a network drive and the
    network drive is disconnected the file handle remains open. when i try to
    close, dispose, or closehandle it in the finaly clause it fails to close
    because it tries to flush the buffer in all of those cases. is this a bug in
    .net file io? is there a work around? do i need to write a dll in c that
    does file io to get around this? is there a workaround in .net? i also tried
    unlocking the file stream in the finaly clause and it still keeps the file
    open. is there any way tof orce a filestream to close a file without trying
    to flush the buffer in .net?

    here is the test case, i put break points in all the catch and finally
    clauses. in all cases i am unable to close the file when it throws error
    because i disconnect the network drive while it is in the middle of writing
    out the file.

    using System;
    using System.Runtime.InteropServices;

    namespace cleanclose
    {
     class Class1
     {

      [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
      public extern static bool CloseHandle(IntPtr handle);

      static void Main(string[] args)
      {
       string strTempPath = @"\\123.123.123.123\netdistest\mb30file.txt";
       System.Text.StringBuilder sb = new System.Text.StringBuilder(100000000);
       for(int i=0;i<100000000;i++)
       {
        sb.Append("0");
       }
       string request = sb.ToString();
       System.IntPtr lasthandle = new System.IntPtr(-1);
       while(true)
       {
        System.IO.FileStream fs = null;
        System.IO.StreamWriter fr = null;
        try
        {
         fs=new System.IO.FileStream(strTempPath, System.IO.FileMode.Create);
         fr=new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
         lasthandle = fs.Handle;
        }
        catch
        {
         try
         {
          CloseHandle(lasthandle);
         }
         catch(Exception eech)
         {
          int i234222 = 23 + 23;
         }

        }
        try
        {
         fr.Write(request);
        }
        catch(Exception ee1)
        {
         int i23 = 23 + 23;
        }
        finally
        {
         try
         {
          fr.Close();
         }
         catch(Exception ee2)
         {
          try
          {
           fs.Close();
          }
          catch(Exception ee3)
          {
           try
           {
            CloseHandle(fs.Handle);
           }
           catch(Exception ee4)
           {
            try
            {
             fs.Unlock(0, fs.Length);
            }
            catch
            {
             int isdfw = 23 + 23;
            }
           }
          }
         }
        }
       }
      }
     }
    }


  • Next message: Daniel: "is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when it is closed in the finaly block?"

    Relevant Pages