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

Getting started

Start a new C# project called “TimeDelete”. You should be able to use any Express or full edition of Visual Studio as no recent .NET features are used. For the same reason conversion to any other language is very easy.

Switch to the form editor and add three buttons with the captions “Install/Repair Time delete”. “Delete Time Expired Files” and “Exit”.

fig1
The first three buttons


The first code to create is the installation procedure which simply adds a shortcut to the correct directory targeting the running version of the program. We need the location of the Send To directory for the current user and the current directory where the executable is stored – and both are remarkably easy to get, with the help of the Environment class:

private void button1_Click(
object sender, EventArgs e)
{
string SendTo =Environment.
GetFolderPath(Environment.
SpecialFolder.SendTo);
string ProgramPath=
Environment.CurrentDirectory;

Now we are ready to create the shortcut – but there is a problem. Even though there are lots of helpful classes in the .NET Framework there doesn’t seem to be one that lets you work with shortcuts. There are a number of solutions to this problem, including using pInvoke to call low-level API functions, but arguably the simplest is to use an ActiveX class library that is generally available as part of the Scripting support (for both JScript and VBScript). To use it first load a reference to a COM object - Windows Script Host Object – and add:

using IWshRuntimeLibrary;

Now we can create a shortcut in exactly the same way that we would using VBScript or JScript. First create a WhsShell object:

 WshShellClass WshS = new WshShellClass();
IWshShortcut Shortcut =
(IWshShortcut)WshS.CreateShortcut(
SendTo + @"\TimeDelete.lnk");
Shortcut.TargetPath = ProgramPath +
@"\TimeDelete.exe";
Shortcut.WindowStyle = 7;
Shortcut.Save();
}

At this point we have added a shortcut that adds the TimeDelete option to the SendTo menu.

Before moving on to the next stage it’s worth creating the code for the Exit button but the button that actually does the delete is easier to understand after a description how the rest of the program works:

private void button2_Click(
object sender, EventArgs e)
{
Application.Exit();
}

fig2

The new SendTo menu item

<ASIN:0735621632>

<ASIN:0321485890>

<ASIN:0321245660>



Last Updated ( Saturday, 11 July 2009 )