If An Exception During using "(StreamWriter..."

  • Thread starter Thread starter Gary Brown
  • Start date Start date
G

Gary Brown

Hi,

If there is an exception during writing a file with StreamWriter how
do you abort the creation of the new file? Due to introducing a bug
an existing file was lost (backed-up, fortunately) when the "Write"
hit the bug. The incomplete file closed and overwrote its
predecessor. How do I prevent this from happening again? The
catches didn't.

The abbreviate code is:

using (StreamWriter SW = new StreamWriter(Filepath))
{
try
{
Write(SW);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "...");
throw;
}
}
catch (Exception e) { MessageBox.Show(e.Message, "..."); }


Thanks,
Gary
 
First backup your original file. write your file in a temp location, and
then copy it over to the new location. Now what happens at copy over,
restore from backup. So the file is place safe, if your back failed, that
might a situation. So the other option is to store incremental parts of
information in separate files, so not the entire program's data is lost, and
some recovery is available. Again if you access to many files it might be
slow to program, so essentially its balance of what's important to you.

The software can only to a extent provide fail safe mechanism, using
computers you need to learn to backup, else you are in trouble.

Did I answer your question or ramble too much??

VJ
 
If there is an exception during writing a file with StreamWriter how
....
First backup your original file. write your file in a temp location, and
then copy it over to the new location. Now what happens at copy over,
....

That is what I was afraid of. Some OS's (VMS? TOPS-20?) allowed
you to just bail out and the file system was none the wiser.
Did I answer your question or ramble too much??

No, that was fine.

Thanks,
Gary
 
If there is an exception during writing a file with StreamWriter how
...

That is what I was afraid of. Some OS's (VMS? TOPS-20?) allowed you
to just bail out and the file system was none the wiser.

Vista apparently has a transactional file system, so it might be possible
there.

Hans Kestin
 
Back
Top