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

D

Daniel

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;
}
}
}
}
}
}
}
}
}
 
W

Wayne

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 said:
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;
}
}
}
}
}
}
}
}
}
 
D

Daniel

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 said:
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 said:
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;
}
}
}
}
}
}
}
}
}
 
W

Willy Denoyette [MVP]

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 said:
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;
}
}
}
}
}
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top