using System; using System.IO; using System.Management; using System.ServiceProcess; using System.Timers; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Threading; using System.Diagnostics; using System.Text; namespace WinServiceSample { public partial class ScheduledService : ServiceBase { protected override void OnStart(string [] strgs) { //add this line to text file during start of service TraceService("start service"); //handle Elapsed event timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); //This statement is used to set interval to 1 minute (= 60,000 milliseconds) timer.Interval = 10000; //enabling the timer timer.Enabled = true; ManagementEventWatcher mwe_creation; WqlEventQuery q_creation = new WqlEventQuery(); q_creation.EventClassName = "__InstanceCreationEvent"; q_creation.WithinInterval = new TimeSpan(0, 0, 2); //How often do you want to check it? 2Sec. q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'"; mwe_creation = new ManagementEventWatcher(q_creation); mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation); mwe_creation.Start(); ManagementEventWatcher mwe_deletion; WqlEventQuery q_deletion = new WqlEventQuery(); q_deletion.EventClassName = "__InstanceDeletionEvent"; q_deletion.WithinInterval = new TimeSpan(0, 0, 2); //How often do you want to check it? 2Sec. q_deletion.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition' "; mwe_deletion = new ManagementEventWatcher(q_deletion); mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion); mwe_deletion.Start(); } internal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e) { TraceService("USB Added"); } internal void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e) { TraceService("USB Removed"); } //This method is used to stop the service protected override void OnStop() { timer.Enabled = false; TraceService("stopping service"); } private void OnElapsedTime(object source, ElapsedEventArgs e) { TraceService("Another entry at "+DateTime.Now); } private void TraceService(string content) { //set up a filestream FileStream fs = new FileStream(@"d:\ScheduledService.txt",FileMode.OpenOrCreate, FileAccess.Write); //set up a streamwriter for adding text StreamWriter sw = new StreamWriter(fs); //find the end of the underlying filestream sw.BaseStream.Seek(0, SeekOrigin.End); //add the text sw.WriteLine(content); //add the text to the underlying filestream sw.Flush(); //close the writer sw.Close(); } private void Filewriter(string message) { //set up a filestream FileStream fs = new FileStream(@"d:\keystrokes.txt", FileMode.OpenOrCreate, FileAccess.Write); //set up a streamwriter for adding text StreamWriter sw = new StreamWriter(fs); //find the end of the underlying filestream sw.BaseStream.Seek(0, SeekOrigin.End); //add the text sw.WriteLine(message); //add the text to the underlying filestream sw.Flush(); //close the writer sw.Close(); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)