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>
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"];

Code Snippet
- protected override void OnStart(string[] args)
- {
- try
- {
- fileWatcherWatchDdriveArticleimagefolder.Path = WatchPath1;
- fileWatcherWatchDDriveMYdataFolder.Path = WatchPath2;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
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
- /// <summary>
- /// This event monitor folder wheater file created or not.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void fileWatcherWatchDDriveMYdataFolder_Created(object sender, System.IO.FileSystemEventArgs e)
- {
- try
- {
- Thread.Sleep(70000);
- //Then we need to check file is exist or not which is created.
- if (CheckFileExistance(WatchPath2, e.Name))
- {
- //Then write code for log detail of file in text file.
- CreateTextFile(WatchPath2,e.Name);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
Code Snippet:
- private bool CheckFileExistance(string FullPath, string FileName)
- {
- // Get the subdirectories for the specified directory.'
- 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;
- }
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();
- }
- }
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();
- }
- }
Code Snippet:
- protected override void OnStop()
- {
- try
- {
- Create_ServiceStoptextfile();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
Step 9: After installation of Window service you can see the following output:


Nupur MehtaPosted Jul 1, 2019, 1:23 AM
How can copy the files created in one folder and paste them in the other folder?
Venkat eshwarPosted Dec 5, 2016, 5:25 PM
Is these two are your file names - articleimg,MYdata ? I did write a service and installed the stop time stamp is working but if i copy my file there is no action!!
j pPosted Oct 3, 2016, 9:33 PM
Thread alee even beginners kniw that is bad code. You should always use asynch pattern at least, or better yet task await pattern. Thia "example" gives the impressiob that thread sleis ok, but it wastes cpu time just doing nothing the threa doesn't yielx its time when you csll sleep. There is nevwr a reason to call sleep. If your programw needs it your design is flawed.
j pPosted Aug 28, 2016, 5:16 AM
Wow you delete comments with valiid concerns about the quality of your example without even responding. This site lacks credibility. Peer review is part of blogging.
Ronrox ThreadPosted Jun 4, 2016, 8:16 AM
Does this work on Network drive?
MaNu GrossoPosted Feb 25, 2016, 5:50 AM
Nice men Thx ! Be careful when you do installutil you need to use "" or it will write System.IO.FileNotFoundException.like this installUtil "C:\Users\<UserName>\Documents\Visual Studio 2015\Projects\FileWatcherWinService\FileWatcherWinService\bin\Debug\FileWatcherWinService.exe" Good job for this !
Rahul PatilPosted Dec 28, 2015, 6:05 AM
Nice
Humayun Kabir MamunPosted Sep 11, 2015, 2:33 AM
Nice...
Santhakumar MunuswamyPosted Sep 7, 2015, 2:34 PM
Good One. Thanks for sharing
Priti KumariPosted Sep 7, 2015, 12:56 PM
Thanks everyone
RakeshPosted Sep 7, 2015, 12:52 PM
Good Explain
Pankaj Kumar ChoudharyPosted Sep 7, 2015, 12:30 PM
Nice Article mam.......
Mohammed IbrahimPosted Sep 7, 2015, 11:52 AM
nice
Shridhar SharmaPosted Sep 7, 2015, 10:58 AM
nice share.
Karthikeyan KPosted Sep 7, 2015, 9:03 AM
Good one...Thanks for sharing us...