PC Review


Reply
Thread Tools Rate Thread

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

 
 
Daniel
Guest
Posts: n/a
 
      28th Sep 2005
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;
}
}
}
}
}
}
}
}
}


 
Reply With Quote
 
 
 
 
Wayne
Guest
Posts: n/a
 
      28th Sep 2005
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?

--
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

"Daniel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
> }
> }
> }
> }
> }
> }
> }
> }
> }
>
>



 
Reply With Quote
 
Daniel
Guest
Posts: n/a
 
      28th Sep 2005
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

"Wayne" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
> --
> 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
>
> "Daniel" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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;
> > }
> > }
> > }
> > }
> > }
> > }
> > }
> > }
> > }
> >
> >

>
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      28th Sep 2005
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.

"Daniel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
> }
> }
> }
> }
> }
> }
> }
> }
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get File sharing to work, but got internet to work. =?Utf-8?B?Q3BpZXJzd2lt?= Windows XP Networking 2 6th Jan 2006 07:12 PM
Working without word work file, memory nearly full. Save work. =?Utf-8?B?RGF2ZSBXaGl0?= Microsoft Word Document Management 1 17th Nov 2004 09:39 PM
How I can work properly with a PDF File converted to work documen. =?Utf-8?B?am9oYW5uYQ==?= Microsoft Word Document Management 1 23rd Oct 2004 08:17 AM
file:// links won't work in XP, work in Win2000 Andrea Martin - Works MVP Windows XP Security 3 23rd Sep 2004 07:54 PM
http:// not work; file:// and ftp:// work Ed Windows XP Internet Explorer 5 25th Nov 2003 03:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:11 AM.