Link to home
Start Free TrialLog in
Avatar of ashley2009
ashley2009

asked on

How to make a Windows Service that deletes file every 15 minutes with C#?

I can make a project with Windows Service option from Visual Studio, but I cannot figure out how I can make one that will delete file every 15 minute or 1 hour. Also how can I install that? My environment is C# .NET Windows  and Visual Studio 2010/2012. Please help.
Avatar of Wayne Michael
Wayne Michael
Flag of United States of America image

You need to track a start date time and then check the difference between the current time in a loop until it is greater than so many minutes.  

then reset the start time with current time and go back to a loop again.

You will need to sleep your thread during the looping to make sure it doesn't eat your processor up between cycles.
Here is some date math code I found that might help you.

DateTime startTime = DateTime.Now;

DateTime endTime = DateTime.Now.AddSeconds( 75 );

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

Open in new window

Avatar of ashley2009
ashley2009

ASKER

Hello,

actually I need a sample of the cs file that would show how I can call a method from a windows service cs file page. It does not have to be even delete file. Just a Windows service. I cannot figure out where to insert a method like "DeleteFile()" or "GetDataFromTable()". Should I insert a method, say that has to execute say every day or or every week at OnStart()? Would you please give me some code that shows where I will insert my method that will be executing say every day.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Michael
Wayne Michael
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial