This article is useful when we need to monitor that the file is created or not after completion of job or any scheduled based task on server. I will create service for this kind of task which monitors the file which is created or not at a particular location.
Watch File creation at a specified location
If you are new in window service just go through below link.
Now we will see how to create service to watch folder.
Step 1: You need to add file path in App.config file.
Here I have added two file paths because I want to watch both the locations.
Code Snippet
- <appSettings>
- <add key="WatchPath1" value="D:\\articleimg" />
- <add key="WatchPath2" value="D:\\MYdata" />
- </appSettings>
Step 2: After that you need to drag and drop
fileWatcher control from toolbox to service design section.
I have added two fileWatcher control for watch two locations of my computer.
Step 3: Now we need to write some code in Service.cs section.
Initially you need to access both the files from App.config file. Before writing the following code you need to add reference of System.Configuration in your window application.
Then add
System.Configuration namespace in
Service1.cs, after that you can write the following code.
Code Snippet - string WatchPath1 = ConfigurationManager.AppSettings["WatchPath1"];
- string WatchPath2 = ConfigurationManager.AppSettings["WatchPath2"];
Step 4: After completing the preceding steps you need to allocate value to the
fileWatcher on start of service like the following image.
Code Snippet
- protected override void OnStart(string[] args)
- {
- try
- {
- fileWatcherWatchDdriveArticleimagefolder.Path = WatchPath1;
- fileWatcherWatchDDriveMYdataFolder.Path = WatchPath2;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
Step 5: Now you need to work on fileWatcher created event. So I have fired an event in service.cs constructor. As I said I have used two fileWatcher, so I have fired event corresponding to each fileWatcher.
Step 6: Now I am going to describe code inside fileWatcher which I have written in fileWatcher created event. Here I will discuss only one file watcher code because I will use same piece of code in another fileWatcher event.
Code Snippet -
-
-
-
-
- void fileWatcherWatchDDriveMYdataFolder_Created(object sender, System.IO.FileSystemEventArgs e)
- {
- try
- {
- Thread.Sleep(70000);
-
- if (CheckFileExistance(WatchPath2, e.Name))
- {
-
- CreateTextFile(WatchPath2,e.Name);
- }
-
- }
- catch (Exception ex)
- {
-
- throw ex;
- }
-
- }
Now I will show you
CheckFileExistance method. In this method I have checked that directory and file exists or not and it returns true and false accordingly.
Code Snippet:
- private bool CheckFileExistance(string FullPath, string FileName)
- {
-
- bool IsFileExist = false;
- DirectoryInfo dir = new DirectoryInfo(FullPath);
- if (!dir.Exists)
- IsFileExist = false;
- else
- {
- string FileFullPath = Path.Combine(FullPath, FileName);
- if (File.Exists(FileFullPath))
- IsFileExist = true;
- }
- return IsFileExist;
-
-
- }
Now I will discuss about
CreateTextFile method. This method called when above method returns true value.
CreateTextFile method used to create text file for maintaining the status of file creation.
Code Snippet:
- private void CreateTextFile(string FullPath, string FileName)
- {
- StreamWriter SW;
- if (!File.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txtStatus_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))
- {
- SW = File.CreateText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txtStatus_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"));
- SW.Close();
- }
- using (SW = File.AppendText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txtStatus_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))
- {
- SW.WriteLine("File Created with Name: " + FileName + " at this location: " + FullPath);
- SW.Close();
- }
- }
Step 7: Now I am writing a method which maintains the service stop status in text file. In this method I have created a text file and maintained service stop time.
Code Snippet:
- public static void Create_ServiceStoptextfile()
- {
- string Destination = "D:\\articleimg\\FileWatcherWinService";
- StreamWriter SW;
- if (Directory.Exists(Destination))
- {
- Destination = System.IO.Path.Combine(Destination, "txtServiceStop_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
- if (!File.Exists(Destination))
- {
- SW = File.CreateText(Destination);
- SW.Close();
- }
- }
- using (SW = File.AppendText(Destination))
- {
- SW.Write("\r\n\n");
- SW.WriteLine("Service Stopped at: " + DateTime.Now.ToString("dd-MM-yyyy H:mm:ss"));
- SW.Close();
- }
- }
Then I will call above method on stop event of this service.
Code Snippet: - protected override void OnStop()
- {
- try
- {
- Create_ServiceStoptextfile();
- }
- catch (Exception ex)
- {
-
- throw ex;
- }
-
- }
Step 8: After that we need to install this service. You can follow my previous article steps for installing window service.
How to create Window service. Step 9: After installation of Window service you can see the following output: