How to Create Our Own File Watcher

Total Page:16

File Type:pdf, Size:1020Kb

How to Create Our Own File Watcher

How to create our own File Watcher

First select Visual C# Windows Service After the project starts go to codeing part by right click and select View Code.

And type the below code 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; namespace filewatcher { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); }

StreamWriter str = new StreamWriter("D:\\watcher.txt", true); FileSystemWatcher fsw = new FileSystemWatcher ("D:\\filewatcher");

protected override void OnStart(string[] args) { fsw.Filter="*.*"; fsw.EnableRaisingEvents = true; fsw.Created += new FileSystemEventHandler(fsw_Created); fsw.Deleted += new FileSystemEventHandler(fsw_Deleted); fsw.Renamed += new RenamedEventHandler(fsw_Renamed); }

void fsw_Renamed(Object sender, RenamedEventArgs e) { str.WriteLine(e.OldName+" has been renamed to "+e.Name); str.Flush(); }

void fsw_Deleted(Object sender, FileSystemEventArgs e) { str.WriteLine(Environment.UserName+" has been deleted "+e.Name+" from "+fsw.Path); str.Flush(); }

void fsw_Created(Object sender, FileSystemEventArgs e) { str.WriteLine("A new file " + e.Name + " has been created in "+fsw.Path); str.Flush(); }

protected override void OnStop() { str.WriteLine("service stoped on " + DateTime.Now.ToString()); str.Close();

} } } After the code is complete, compile it, it will not run but, it will give an error like this…

It will not give any output bcoz Services does not have any GUI, it runs internally….

After the code is complete come again in Design page & add installer by right clicking on the page. After you add installer you will get page name ProjectInstaller1.cs  You will get 2 controls: 1. serviceProcessInstaller1 2. serviceInstaller1 1. serviceProcessInstaller1

we need to change the Account property to LocalSystem of the control. As we want the service to be handled by System itself & not by users. 2. serviceInstaller1

Change the Description if you want to…. Change & Remember the DisplayName bcoz that name will display in Services. you can set StartType to Automatic if you want your service to start when OS starts. (start at boot up). After the coding is over open the CMD PROMPT of Visual Studio. When you open the CMD it will look like this…

Change the directory in CMD where the exe file is created of the project. To change the directory use cd (path of the exe file of project) Now its very intresting part of the project, to install the service to your system which you created……

It is one line code i.e. installutil –i (name of service).exe

Now the service will get install in your system & you will get message at last “The Transacted install has completed”, to check this out open services in Control Panel

Control Panel=>Administrative Tools=> Services

You will get all the services which is installed in your system all the services Started & Stoped…. Now check your installed service, by the Display name…. When you double click on the service, you will get the Properties of that services….

Before starting the service of file watcher you should create a folder of name filewatcher as per the above coding in D: drive.

Now when you start the service 1 file will created in the D: drive name watcher.txt as per the coding..

Now this .txt file will note all the event which will be performed in the filewatcher folder.

Just check-it out……

And to uninstall the service installutil –u (name of service).exe

But, make sure while uninstalling service, the service should be Stopped…

That’s all Guys, Thanks for your support, With Regards, ~~~ CHIRAG OZA ~~~

Recommended publications