Closing File Stream
-
I have the following and have the error when I try to close the FileStream. It says cannot access fs as it no longer exist. something like that. But I did not delete it.. I have this problem when I have the close the Stream Reader statement. Also, shouldn't the if statement work in checking if the FileStream still exist? I'm Confused :confused:
string path = @"c:\TrayCount.txt"; fs = new FileStream( path, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.ReadWrite); sr = new StreamReader(fs); sw = new StreamWriter(fs); if(sr!=null) sr.Close(); if(sw!=null) sw.Close(); if(fs!=null) fs.Close();
-
I have the following and have the error when I try to close the FileStream. It says cannot access fs as it no longer exist. something like that. But I did not delete it.. I have this problem when I have the close the Stream Reader statement. Also, shouldn't the if statement work in checking if the FileStream still exist? I'm Confused :confused:
string path = @"c:\TrayCount.txt"; fs = new FileStream( path, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.ReadWrite); sr = new StreamReader(fs); sw = new StreamWriter(fs); if(sr!=null) sr.Close(); if(sw!=null) sw.Close(); if(fs!=null) fs.Close();
-
I have the following and have the error when I try to close the FileStream. It says cannot access fs as it no longer exist. something like that. But I did not delete it.. I have this problem when I have the close the Stream Reader statement. Also, shouldn't the if statement work in checking if the FileStream still exist? I'm Confused :confused:
string path = @"c:\TrayCount.txt"; fs = new FileStream( path, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.ReadWrite); sr = new StreamReader(fs); sw = new StreamWriter(fs); if(sr!=null) sr.Close(); if(sw!=null) sw.Close(); if(fs!=null) fs.Close();
well I checked the documentation for this and indeed it is right ... closing the streamreader/writer will close the stream associated with it. For some wierd reason till today I always use to assosciate one filestream to one reader / writer ... gladly never faced this problem I dont know if they got the implementation right. you are exclusively stating that you want FileShare mode and when u do that u expect that this filestream is going to be shared. I think they should have provided a reference count on SHARED system resource or atleast in IOStream .... espescially when it is explicitly indicate that u expect the filestream to be shared. might be worth taking up this to msdn Pushkar Pathak
-
well I checked the documentation for this and indeed it is right ... closing the streamreader/writer will close the stream associated with it. For some wierd reason till today I always use to assosciate one filestream to one reader / writer ... gladly never faced this problem I dont know if they got the implementation right. you are exclusively stating that you want FileShare mode and when u do that u expect that this filestream is going to be shared. I think they should have provided a reference count on SHARED system resource or atleast in IOStream .... espescially when it is explicitly indicate that u expect the filestream to be shared. might be worth taking up this to msdn Pushkar Pathak
The other thing i'm still confused is that of the if statement. Shouldn't if( fs!=null) be enough to check that the filestream has already closed?
-
The other thing i'm still confused is that of the if statement. Shouldn't if( fs!=null) be enough to check that the filestream has already closed?
Why should closing a
FileStream
object set your reference tonull
? CallingClose
on aFileStream
means calling a method of an object, like every other method call. For your reference to be null after the call I'd assume there'd have to be the linethis = null;
somewhere inFileStream.Close()
and I strongly doubt that this works... mav -
Why should closing a
FileStream
object set your reference tonull
? CallingClose
on aFileStream
means calling a method of an object, like every other method call. For your reference to be null after the call I'd assume there'd have to be the linethis = null;
somewhere inFileStream.Close()
and I strongly doubt that this works... mavThe Close () method in turns calls the Dispose() method and thats the reason why the filestream object is garbage collected... here is the msdn reference for it http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassclosetopic.asp[^] Pushkar Pathak
-
The Close () method in turns calls the Dispose() method and thats the reason why the filestream object is garbage collected... here is the msdn reference for it http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassclosetopic.asp[^] Pushkar Pathak
Calling Dispose() doesn't make the object eligible for garbage collection. An object will be GC'ed only if there are no live references to it. Regards Senthil _____________________________ My Blog | My Articles | WinMacro