Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
ya it's help tel to me. i want message when service starts . can u tel how it's work code ..
Posted
Updated 27-Sep-11 22:46pm
v2
Comments
Mehdi Gholam 28-Sep-11 2:00am    
Please do not repost, check the answers to the previous post.

For every 1 minute you want to delete your files from your directory m i right..?
so you need to create a timer and set its interval to your time duration on which you want to clear your directory.
and start that timer on windows service will start.
so when timer Elapsed event is call at that time you can call your deletefiles method so your files will be deleted..
please see the below solutions it will give you a good idea what i mean to say you...

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer t;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Display();
        }

        protected void CreateTimer()
        {
            t = new System.Timers.Timer();
            t.Interval = (10000) * 6;  // here i will set it to 60 secs to call deletefiles method
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
        }
        protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DeleteFiles();
        }

        protected override void OnStop()
        {
           
        }

        public void Display()
        {
            MessageBox.Show("Service Started");
            CreateTimer();
            t.Enabled = true;
            t.Start();

        }
        protected void DeleteFiles()
        {
            string[] Files = Directory.GetFiles(@"YOUR DIRECTORY PATH");
            for (int i = 0; i < files.length; i++)
            { 
                File.Delete(Files[i]); 
            }
        }
    }
}


Hope this will help you...
 
Share this answer
 
v4
Comments
Tejas Vaishnav 29-Sep-11 0:41am    
Dont forgot to accept and rate it..
SAISAGAR nalla 30-Sep-11 2:23am    
hi frnd..it's really working for me. thanq very much.. and i want small thing " after one min files are deleted, it's nice. but here i want notification to user like files are deleted. " how can i handle this one. user getting pop up msg r notification any thing...
Hello Friend...
First of all see this link:

Simple Windows Service Sample[^]

After installing your service you need to start it. You can do this by following these steps:

1) Open Control Panel
2) Then Administrative Tools
3) Services
4) Find your service
5) Right click on it and select start

6) Now your service will start and be working.

Alternatively you can:
1) Press WindowsKey + R
2) In Run type in services.msc
3) Then hit enter on your keyboard and it will open the service manager
4) Follow steps 4 and 5 of the instructions above to start your service.


Or you can also start your service with the compiled code of your setup project.

However, before that code can run your service must be installed on your computer.

You can also make your service as start automatically when Windows starts so that it will start after you restart your computer. You may want to research this on Google.


I hope this will help you..
 
Share this answer
 
v3
Comments
SAISAGAR nalla 28-Sep-11 3:32am    
ya it's help tel to me. i want message when service starts . can u tel how it's work code ..
SAISAGAR nalla 30-Sep-11 2:24am    
hi frnd..it's really working for me. thanq very much.. and i want small thing " after one min files are deleted, it's nice. but here i want notification to user like files are deleted. " how can i handle this one. user getting pop up msg r notification any thing...
SAISAGAR nalla
ya it's help tel to me. i want message when service starts . can u tel how it's work code ..


1) Add Reference of System.Windows.Form to your project
> Go To reference Right Click Choose Add reference
> Chose from .net select System.Windows.Form hit enter

2) add one Method to your Windows Service like this
C#
public void display()
{
    System.Windows.Forms.MessageBox.Show("Service Start");
}


3) Call that Method in your Service onStart Method like this
C#
protected override void OnStart(string[] args)
{
    display();
}


so when you start your service at that time it will show you a messagebox with message like "Service Start"
 
Share this answer
 
Comments
SAISAGAR nalla 28-Sep-11 23:52pm    
i Created services and its running fine.. my actual task is " deleting files in a directory after one min " , i completed. i wrote code like this way
in onstart() method
-----------------------------------------------------
string[] Files = Directory.GetFiles(My DIRECTORY NAME);
for (int i = 0; i<files.length; i++)
="" {
="" if="" (file.exists(files[i]))
="" (datetime.now.subtract(file.getcreationtime(files[i])).totalminutes="">= 1)
{
File.Delete(Files[i]);
}
}
}
--------------------------------------------------
When first time SERVICE STARTED files are deleted. thats no problem.... after deleting all file i want to add some more files in directory. but newly added files are not deleted. but still service running but not files deleted. what can i do..
Tejas Vaishnav 29-Sep-11 0:35am    
Hello friend for your problem i also add new solutions please check it..

and also if my all solutions will help you then please accept it as answers and also rate it..
SAISAGAR nalla 30-Sep-11 2:24am    
hi frnd..it's really working for me. thanq very much.. and i want small thing " after one min files are deleted, it's nice. but here i want notification to user like files are deleted. " how can i handle this one. user getting pop up msg r notification any thing...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900