Let's say we have the following specifications :
The specifications are very simple and we will probably start thinking to a windows service that would be launched in an automatic manner. This service could then use a FileSystemWatcher to do its job. Let's create our service
using System.ServiceProcess; namespace MyWindowsService { public partial class MyWindowsService : ServiceBase { public MyWindowsService() { InitializeComponent(); this.ServiceName = "MyPersonalService"; } protected override void OnStart(string[] args) { } protected override void OnStop() { } } } This class exposes a parameterless constructor and two methods OnStart and OnStop. These are the two methods that will be called when the service is being started (or stopped) by Windows. Note that automatically, when a service is being started or stopped, Windows will log an entry in the event viewer. Let's check this ! Let's deploy our (empty) serviceTo do so, we'll need to create a setup project and some installation actions to parameter the way the service is started.
We will need now to control the way the service will be started and to specify it is a service. For that we have two solutions, either we rely on the service itself, or we write it ourself. We will here only see how to create the installer thru the service itself. Creating the installer's action class via the Service class itself
Let's now complete the installerNow we can complete the installer to add the installer actions we have just created.
Now we just need to compile and to install the service to check that it is working.
Let's check the behaviourAs I have said before a service will log some information when being started and stopped. We can so go in the "Services" management window.
Note that even if we have put the "start type" to "Automatic", you must start the service manually. It would start automatically only after the next windows reboot ! Let's start the service and check in the Event Viewer. We can see two new events saying respecively
So far so good ! Let's complete our service to let him workNow that we have a functional service, we can complete him to work. We wanted him to monitor a folder. Let's modify our service and do that this way: using System.IO; using System.ServiceProcess; namespace MyWindowsService { public partial class MyWindowsService : ServiceBase { public MyWindowsService() { InitializeComponent(); this.ServiceName = "MyPersonalService"; } protected override void OnStart(string[] args) { //1. Let's check only txt files in a specific folder string path = @"c:\Temp\MyFolderToMonitor"; string filter = "*.txt"; FileSystemWatcher watcher = new FileSystemWatcher(path, filter); //2. We want information about the filename watcher.NotifyFilter = NotifyFilters.FileName; //3. Let's register the creation event only, and let's start the monitring watcher.Created += new FileSystemEventHandler(watcher_Created); watcher.EnableRaisingEvents = true; } /// /// When a file is created, we'll just add an entry in the EventLog /// Note that this.EventLog will log to the Application Log, /// with the name of the service as source. /// To log in another log, you should create yourself an EventLog. /// private void watcher_Created(object sender, FileSystemEventArgs e) { string message = string.Format("{0} : {1}", e.ChangeType, e.Name); this.EventLog.WriteEntry(message); } protected override void OnStop() { } } } Now we can just rebuild and reinstall the service. We can then start it, create a file in the "Temp\FolderToMonitor" folder and stop the service. If we now look to the EventViewer, what can we see ? Three events :
And here we are ! We now have a fully functional windows service. |
We try to put solutions for the problems you would face in your daily life of being a developer. Keep visiting this website regularly...
Topics
ADO.Net
(1)
Algorithms
(6)
Apache
(1)
Apple
(1)
ASP.net
(3)
Auth
(1)
Bash Shell Script
(1)
C#.Net
(33)
C++
(1)
database
(1)
Design Pattern
(2)
Distributed Messaging System
(1)
docker
(2)
DOS Commands
(3)
FUN-n-Games
(1)
git
(1)
golang
(1)
Google
(1)
Hangouts
(1)
heidisql
(1)
Homebrew
(1)
HTML
(1)
IIS
(1)
Java
(2)
Javascript
(7)
Jmeter
(1)
JQuery
(1)
Kafka
(1)
Keyboard
(1)
Linux - Kubuntu
(1)
Macbook
(1)
macbook pro
(1)
macOS
(1)
MacOSX
(1)
mariadb
(1)
Meet
(1)
Microsoft Office
(1)
MS SQL Server
(25)
mysql
(2)
Networking
(2)
Node.js
(2)
NVM
(1)
OOPs
(5)
Operating Systems
(3)
OSX
(2)
Postman
(1)
Regular Expression
(2)
Research Papers
(1)
Rx
(1)
Shortcuts.
(1)
Silverlight
(1)
sql
(2)
SQL Server 2014
(2)
SSRS
(3)
Team Foundation Server and Client
(1)
TFS
(1)
Visual Studio
(1)
WPF
(25)