Time to delete
Written by Harry Fairhead   
Saturday, 11 July 2009
Article Index
Time to delete
TimeDelete in C#
User interface
The delete list
Doing the Delete
Exception handling

The finer points

fig4

Time delete in action

You now have a complete program but there are lots of things that can go wrong. In particular if it attempts to delete a file that doesn’t exist the whole thing crashes. The solution is to use exception handing. Change each of the delete commands:

System.IO.File.Delete(Line[0]);

to read:

try
{
System.IO.File.Delete(Line[0]);
}
catch { }

The catch part of the expression usually contains instructions to deal with the problem but in this case all we really need to do is to move on after failing to delete a file because it has already been deleted. You might like to add a message box that tells the user what has happened.

Another problem is in the block of code that renames the final files. The first time the program is run there isn’t a $$$ file so the delete has to be surrounded by a try-catch:

try
{
System.IO.File.Delete(ProgramPath +
@"\DeleteData.$$$");
}
catch { }

 

A much more subtle problem is hidden in the line:

System.IO.File.Move(ProgramPath +
@"\DeleteData.T$$", ProgramPath +
@"\DeleteData.TDD");

Think for a moment what happens if all of the files in the delete list are passed their date and have all been deleted. In this case there is no .T$$ file to rename to .TDD and in fact the .TDD file shouldn’t exist. The solution is another try-catch:

try
{
System.IO.File.Move(ProgramPath +
@"\DeleteData.T$$", ProgramPath +
@"\DeleteData.TDD");
}
catch { }

There are probably other similar, rarely occurring, conditions that need to be handled but this is what extended debugging in a beta test phase is all about. The program with the addition of these try-catch clauses works to the point that I haven’t seen it crash.

Where next

The TimeDelete application isn’t particularly good looking and improving its user interface is probably a major task. However it has already proved its worth to me and I can’t imagine not having a time delete facility. Its real purpose here is to see how best to implement such a facility and this is really the big question. Is this the best way to do the job?

If you can think of a better way email me Harry.Fairhead@i-programmer.info.

If you would like the code for this project then register and email your request to code@i-programmer.info

 

<ASIN:0201733978>

<ASIN:1400052920>

 

 

 



Last Updated ( Saturday, 11 July 2009 )