Time to delete
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 delete list

Now we have to move on to consider how to record the files that the user wants to delete. When the user selects a group of files and sends them to the TimeDelete application it starts running with an array of arguments that specify each file. The user is presented with the form and can select the time to delete and whether or not an “are you sure” prompt is shown before the file is deleted. They then click the OK button and the application has to create a record of those files.

I made an early design decision that the file holding the files to be deleted would be stored in the same directory as the application. This caused a problem because when the application is run from the SendTo menu its current directory is set to the one above the directory that the files are stored in and not the directory it was started from. The solution is to read the target directory information from the link stored in the SendTo directory. First we get the SendTo directory:

private void button4_Click(
object sender, EventArgs e)
string SendTo = Environment.

Next we get the shortcut:

WshShellClass WshS = new WshShellClass();
IWshShortcut Shortcut =
SendTo +@"\TimeDelete.lnk");

It generally isn’t appreciated that if you create a Shortcut object in this way it is loaded with all of the data in any existing shortcut. Hence we can easily get the target directory:

string ProgramPath = System.IO.Path.

Now that we have the location we can start to process the data. First we need to work out the date the files need to be deleted on. We need an array of number of days corresponding to each possible item in the listbox:

int[] intervals ={ 1, 7, 30, 365 };

There is scope for customising this but one day, week, month, year seems a reasonable initial choice. Next we need to build up a string containing:

filename,date to delete, prompt

for each file to be added to the data file. The most efficient way of doing this is to use a StringBuilder object:

StringBuilder buffer =
new StringBuilder(1024);

We also need to process each of the arguments in turn:

for (int i = 1; i < m_args.Length; i++)

It is easy to construct a TimeSpan object set to the number of days to deletion:

 TimeSpan interval = new TimeSpan(
0, 0, 0, 0);

To get a delete date we need to add this TimeSpan to the current data i.e. DateTime.Now and build up the line specifying what to delete and when in the StringBuilder:

buffer.Append(m_args[i] + "," + 
ToShortDateString() + "," +
checkBox1.Checked.ToString() +

This looks like a horribly long and complicated line of code but it’s very simple if you take it a chunk at a time. The first item is arg[i] which is just the complete path to the file to be deleted, the second item is the date and time with the TimeSpan object added to it and converted to a short date string. Finally we have the True or False corresponding to the state of the checkbox. Each item is separated by a comma and the line ends with a NewLine. When the for loop ends the StringBuilder object has such a line for each file the user selected. Now everything is ready to write the new data to the data file and this is remarkably easy:

ProgramPath + @"\DeleteData.TDD",

The static File object has a number of very easy-to-use methods that make it very quick to build an application that uses files to store and retrieve data. The AppendAllText method either creates or opens the existing file and stores all of the data in the specified string at the end of the file. You don’t even have to bother closing it. It’s worth comparing this to alternative methods of writing to a file using say VB OPEN commands or even using a Streamwriter – this is much simpler.



Last Updated ( Saturday, 11 July 2009 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.