How to Create Our Own File Watcher

How to Create Our Own File Watcher

<p> How to create our own File Watcher</p><p>First select Visual C# Windows Service After the project starts go to codeing part by right click and select View Code.</p><p>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(); }</p><p>StreamWriter str = new StreamWriter("D:\\watcher.txt", true); FileSystemWatcher fsw = new FileSystemWatcher ("D:\\filewatcher");</p><p> 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); }</p><p> void fsw_Renamed(Object sender, RenamedEventArgs e) { str.WriteLine(e.OldName+" has been renamed to "+e.Name); str.Flush(); }</p><p> void fsw_Deleted(Object sender, FileSystemEventArgs e) { str.WriteLine(Environment.UserName+" has been deleted "+e.Name+" from "+fsw.Path); str.Flush(); }</p><p> void fsw_Created(Object sender, FileSystemEventArgs e) { str.WriteLine("A new file " + e.Name + " has been created in "+fsw.Path); str.Flush(); }</p><p> protected override void OnStop() { str.WriteLine("service stoped on " + DateTime.Now.ToString()); str.Close();</p><p>} } } After the code is complete, compile it, it will not run but, it will give an error like this…</p><p>It will not give any output bcoz Services does not have any GUI, it runs internally….</p><p>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</p><p> 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</p><p>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…</p><p>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……</p><p>It is one line code i.e. installutil –i (name of service).exe</p><p>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</p><p>Control Panel=>Administrative Tools=> Services</p><p>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….</p><p>Before starting the service of file watcher you should create a folder of name filewatcher as per the above coding in D: drive.</p><p>Now when you start the service 1 file will created in the D: drive name watcher.txt as per the coding.. </p><p>Now this .txt file will note all the event which will be performed in the filewatcher folder.</p><p>Just check-it out……</p><p>And to uninstall the service installutil –u (name of service).exe</p><p>But, make sure while uninstalling service, the service should be Stopped…</p><p>That’s all Guys, Thanks for your support, With Regards, ~~~ CHIRAG OZA ~~~</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us