Monday, August 21, 2006 1:17 PM
We are facing a bug in which we are writing data to the excel file in the floppy and the floppy size is full it raise an “IOException” (memory full), in this case I am not able to close the StreamWriter object because StreamWriter Close () function internally call the Flush () method.
But after sometimes or immediately ,i got another IOException of memory full and the call stack show that it raises when the Garbage Collector (GC) call Finalize() function.
I do not wants this second exception , becuase i can not handle this second exception and sometime a process is also runing in the Task Manager after closing the application even i am using the Environment.Exit(0) function in Application for exiting the application.
So my question is can we close the StreamWriter object in this situation If yes then how?
Monday, August 21, 2006 1:39 PMModerator
try the using() statement around the StreamWriter and catch the exception with in it, example:
using (StreamWriter sr = new StreamWriter(File))
catch (IOException e)
the using statement is best practice and allows you to use the object for that block and once its finished using it, it will dispose of the object and close the stream as neccesary.
you may also call the Close() method within the exception if you need to
does this help?
Tuesday, August 22, 2006 3:03 AM
I already done all these things buts its not working....
My main problem is the Finalize() function which call the Flush() function internally.
Tuesday, August 22, 2006 3:17 AMModerator
so the problem is that you cannot write to floppy disk a file because the disk is full correct? of course the best thing to do is to check the file size of the file and the remaining size left on the disk, in this situation.
This can be done as follows:
FileInfo theFileInformation = new FileInfo(File);
DriveInfo theDriveInformation = new DriveInfo("a:");
if (theDriveInformation.AvailableFreeSpace >= theFileInformation.Length)
//we can copy as there is enough room on disk
the DriveInfo class is available in .NET 2.0 and higher.
if you do not want to do this, You can set the "AutoFlush" property to false, to prevent flushing after every Write().
I think the solution here would be to do as the above before doing a write().
Tuesday, August 22, 2006 2:30 PMModeratorNasty problem! What I'd try to do is to start with a FileStream and have it create the file. Then use a StreamWriter created from the FileStream with the StreamWriter(Stream) constructor. When you get the exception, call FileStream.SetLength(0) or FileStream.SetLength(FileStream.Position) to truncate the output buffer.
Hope it works, I can't try it...
Tuesday, August 22, 2006 4:10 PMThanks for the suggestion , but this solution will slow down the application so it is not feasible for us.
Tuesday, August 22, 2006 4:14 PMModerator
the solution will NOT slow down the application, only by a few milliseconds which is not noticable to us.
it is better to check and verify than to immediately get errors.
Please try and get back.
Tuesday, August 22, 2006 4:25 PMYou can not set the length of the StreamWriter in this case it again raise an IOexception
Tuesday, August 22, 2006 4:32 PM
Well friends thanks for yours time i founded that its the open bug of .Net 1.1 and they have the hot fix for this also
You can find out the detail of this bug on this link - http://support.microsoft.com/?kbid=892544
Tuesday, August 22, 2006 4:38 PMModeratorThanks for sharing, however the work around without the hotfix would be as in my response. Glad you got it sorted however
Thursday, August 24, 2006 3:30 AM
I also try yours suggestion of checking the Size of the drive before writing the data but its OK if i am writing the data to the local hard drive but if i am writing the data to the floppy or USB its taking so much time (1-2 sec.) which is not acceptable.
So this solution is only acceptable if you are writing the data to the local drive but in case of floppy or USB i am not supporting this solution in the application where yours target is save every single seconds.
Friday, June 08, 2007 3:02 PMWhat about writing to a temp file on disk or memory cache that is sized to fit your target floppy. once the temp object is full then write it to your floppy.