Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

File Watcher Windows Service in C#

Rate me:
Please Sign up or sign in to vote.
3.26/5 (20 votes)
1 Sep 20042 min read 231.5K   8.2K   88  
This article will give you a peep into creating a basic windows service in C#
using System;
using System.Configuration;
using System.IO;

namespace FileSysWatcher
{
	/// <summary>
	/// Summary description for Util.
	/// </summary>
	public class Util
	{
		public static string logFileName;
		public static string errorLogFileName;

		/// <summary>
		/// Static Constructor
		/// </summary>
		static Util()
		{
			logFileName = GetSetting("LOGFILENAME");
			errorLogFileName = GetSetting("ERRORLOGFILENAME");

			IsDirectoryPresent(StripDirectoryName(logFileName),true);
			IsDirectoryPresent(StripDirectoryName(errorLogFileName),true);
		}


		/// <summary>
		/// Gets The File Name From Specified Path
		/// </summary>
		public static string GetFileNameFromPath(string path)
		{
			string fileName = @"";
			int indexOfLastSlash = 0;
			try
			{
				indexOfLastSlash = path.LastIndexOf(@"\");
				fileName = path.Substring(indexOfLastSlash + 1);
				return fileName;
			}
			catch(Exception ex)
			{
				WriteToErrorLogFile(ex);
				return "";
			}
			finally
			{
			}
		}

		/// <summary>
		/// Gets The Directory Path from the FilePath
		/// </summary>
		public static string StripDirectoryName(string path)
		{
			string direcoryPath = @"";
			int indexOfLastSlash = 0;

			try
			{
				indexOfLastSlash = path.LastIndexOf(@"\");
				direcoryPath = path.Substring(0,indexOfLastSlash);
				return direcoryPath;
			}
			catch(Exception ex)
			{
				WriteToErrorLogFile(ex);
				return "";
			}
			finally
			{
			}
		}


		/// <summary>
		/// Gets Values From The Config File.
		/// </summary>
		public static bool IsDirectoryPresent(string directory,bool create)
		{
			try
			{
				if (!Directory.Exists(directory))
				{
					if (create == true)
					{
						Directory.CreateDirectory(directory);
						return true;
					}
					else
					{
						return false;
					}
				}
				else
				{
					return true;
				}
			}
			catch (Exception ex)
			{
				WriteToErrorLogFile(ex);
				return false;
			}
			finally
			{
			}
		}


		/// <summary>
		/// Gets Values From The Config File.
		/// </summary>
		public static string GetSetting(string val)
		{
			try
			{
				return ConfigurationSettings.AppSettings[val];
			}
			catch (Exception ex)
			{
				WriteToErrorLogFile(ex);
				return "";
			}
			finally
			{
			}
		}


		/// <summary>
		/// Writes the message to the FileSystem Watcher Log File
		/// </summary>
		public static void WriteToLogFile(string message)
		{
			if (IsDirectoryPresent(StripDirectoryName(logFileName),true))
			{
				FileStream fs = null;
				StreamWriter sw = null;
				//string formattedDate;
				string fileName;
				//int indexOfPeriod;

				try
				{
					//formattedDate = "(" + DateTime.Now.ToString("dd - MM - yyyy") + ")";
					//indexOfPeriod = logFileName.LastIndexOf(".");
					fileName = logFileName ; //.Insert(indexOfPeriod,formattedDate);

					fs = new FileStream(fileName,FileMode.Append,FileAccess.Write);
					sw = new StreamWriter(fs);
					sw.WriteLine(message);
				}
				catch (Exception ex)
				{
					WriteToErrorLogFile(ex);
				}
				finally
				{
					if (sw != null)
					{
						sw.Close();
					}

					if (fs != null)
					{
						fs.Close();
					}
					PrintReport.Print();
				}
			}
		}

		/// <summary>
		/// Writes the Exception to the FileSystem Watcher Error Log File
		/// </summary>
		public static void WriteToErrorLogFile(Exception sourceException)
		{
			if (IsDirectoryPresent(StripDirectoryName(errorLogFileName),true))
			{
				FileStream fs = null;
				StreamWriter sw = null;
				try
				{
					fs = new FileStream(errorLogFileName,FileMode.Append,FileAccess.Write);
					sw = new StreamWriter(fs);
					sw.WriteLine("==================================================================");
					sw.WriteLine("ERROR OCCOURED IN:" + sourceException.Source);
					sw.WriteLine("ERROR DESCRPTION:" + sourceException.Message);
					sw.WriteLine("ERROR DESCRPTION:" + sourceException.StackTrace);
					sw.WriteLine("==================================================================");
					sw.WriteLine("");
				}
				catch (Exception ex)
				{
					throw ex;
				}
				finally
				{
					if (sw != null)
					{
						sw.Close();
					}

					if (fs != null)
					{
						fs.Close();
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
A Microsoft Certified Professional with over 2 Yrs experience in .Net. Worked on both Windows-based and Web-based Technologies. Used Cutting Edge Tools such as Visual Studio.Net 2003, Oracle9i. Worked with organisations such as Cygnus Software Pvt. Ltd. and Deloitte Consulting. Presently working with Nihilent Technologies Pvt. Ltd., Pune, India as System Analyst.

Comments and Discussions