Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel - 28 Sep 2005 20:27 GMT
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;
       }
      }
     }
    }
   }
  }
 }
}
}
Wayne - 28 Sep 2005 21:00 GMT
I believe the file being locked is controlled by the machine where it
belongs, this makes sense as it would prevent multiple machines from writing
to it at once. Since you are disconnecting the share your app is unable to
remove the lock (close the file). It no longer has access to the file, so
how could it close it?

Signature

Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

> Is this a bug in .net file io? is there a work around?
>
[quoted text clipped - 107 lines]
> }
> }
Daniel - 28 Sep 2005 21:23 GMT
well if i kill the .net process then the file is freed up and i can write to
it again. so it is a problem inside the .net process holding a lock on a
file and not allowing me to release that lock

> I believe the file being locked is controlled by the machine where it
> belongs, this makes sense as it would prevent multiple machines from writing
[quoted text clipped - 113 lines]
> > }
> > }
Willy Denoyette [MVP] - 28 Sep 2005 23:14 GMT
When the networks is disconnected all you can and should do is call
CloseHandle, you should not try something else, it wont work. It also makes
no sense to wrap CloseHandle in a try block, this function doesn't throw
exceptions.

     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( System.IO.IOException e )
     {
       // check Win32 error
       if (Marshal.getLastWin32Error() == 6) // error code 6 = Invalid
handle
           CloseHandle(lasthandle );
     }

When the handle is closed you can reopen the file when the network is
restored.

Also keep in mind that Windows networks assume reliable connections, and
that you don't disconnect file shares when there are active session.
Willy.

> Is this a bug in .net file io? is there a work around?
>
[quoted text clipped - 107 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.