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

Doing the Delete

The user has to run the TimeDelete application directly, i.e. not from the SendTo menu, to scan the list of files and delete any that have expired. The first thing we need is the path to the data file and this is a matter of reading the Shortcut again to obtain the target path:

private void button3_Click(
object sender, EventArgs e)
{
string SendTo = Environment.
GetFolderPath(
Environment.SpecialFolder.SendTo);
WshShellClass WshS = new WshShellClass();
IWshShortcut Shortcut =
(IWshShortcut)WshS.
CreateShortcut(SendTo +
@"\TimeDelete.lnk");
string ProgramPath =
System.IO.Path.GetDirectoryName(
Shortcut.TargetPath);

Now that we have the path we can open the data file. This time we have no choice but to use a more standard approach to file handling and use a Streamreader – but the File object still provides an easy way to get one:

System.IO.StreamReader FileList=
System.IO.File.OpenText(ProgramPath +
@"\DeleteData.TDD");

We also need to know the current date to compare to the delete dates in the file:

DateTime now = DateTime.Now;

The simplest way of scanning the file is to use a do loop with a test at the end for the end of file condition:

do
{

Reading in a line and parsing it into its separate items couldn’t be easier:

 String[] Line = FileList.
ReadLine().Split(',');

The Split method returns a string array with each item in a separate element. Element [1] specifies the delete date and this is easy to convert to a date and test against the current date:

  DateTime DeleteTime = 
DateTime.Parse(Line[1]);
if (now.CompareTo(DeleteTime) > 0)
{

If the file’s lifetime is up we need to process it. The second element determines if a prompt is to be issued or the file just deleted:

   if (Line[2]=="True")
{
DialogResult action =
MessageBox.Show(
"Delete " + Line[0] + "?",
"TimeDelete",
MessageBoxButtons.YesNo);
if (action == DialogResult.Yes)
{
System.IO.File.Delete(Line[0]);
}
}
else
{
System.IO.File.Delete(Line[0]);
}
}

Notice that we are using the File object to do the delete – what did we do before it was invented?

If the file’s time isn’t up we simply copy its data line to a new file called DeleteDate.T$$:

  else
{
System.IO.File.AppendAllText(
ProgramPath + @"\DeleteData.T$$",
String.Join(",", Line) +
Environment.NewLine);
}
} while (!FileList.EndOfStream);
FileList.Close();

We have to close the stream explicitly in this case. Also notice the use of the Join method to rebuild the line from the separate elements in the string array. When the do loop comes to and end all of the lines of the file have been processed, the files have either been deleted or not according to what the user selected, and all of the data lines corresponding to files that are still in date have been written to the new file.

The final task is to juggle the file names around so that the new file replaces the original. There are lots of ways of doing this but what you have to keep in mind that you don’t want to arrive at a state where there isn’t a backup file on the disc so one way of doing it is:

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

That is, first delete any existing file ending .$$$, then rename the .TDD file to .$$$ to create a new backup, and finally rename the temporary .T$$ file to .TDD. In this way the user has a backup of the list in the .$$$ file and during the swap over there is always one copy of the data left on the disk. You can try to work out what is the worst state the system can be left in if the process crashes before completion but to do the job better takes a lot of effort.

<ASIN:0735625301>

<ASIN:B0018RCAD4>

<ASIN:1590513126>



Last Updated ( Saturday, 11 July 2009 )
 
 

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