File System Wathcher Using Windows Service

How To Install Windos Service:
-----------------------------

To install windows service in command prombt to enter the below line,

installutil C:\mcWebService\bin\Debug\mcWebService.exe


How To Unistall Service:
----------------------

installutil /u C:\mcWebService\bin\Debug\mcWebService.exe

/u --> Uninstall the windows service

How To Check If Service Running / Not:
-------------------------------------

Control Panel->Administrative Tools->Service

check if service is running or not.





Service1.cs:
-----------

protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"c:\servicetest\filesystemwatcher.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("FileSystemWatcher: Service Started"+System.DateTime.Now.Date.ToShortDateString ()+"--->"+System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.Flush();
m_streamWriter.Close();

}

protected override void OnStop()
{
FileStream fs = new FileStream(@"c:\servicetest\filesystemwatcher.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("FileSystemWatcher: Service Stopped" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.Flush();
m_streamWriter.Close();

}

private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
FileStream fs = new FileStream(@"c:\servicetest\filesystemwatcher.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("File Name Changed\t" + e.FullPath.ToString() + "\t" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.WriteLine("File Change Type \t" + e.ChangeType.ToString() + "\t" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());

m_streamWriter.Flush();
m_streamWriter.Close();
}

private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{

FileStream fs = new FileStream(@"c:\servicetest\filesystemwatcher.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("File Newly Created\t" + e.FullPath.ToString() + "\t" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.Flush();
m_streamWriter.Close();
}

private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
FileStream fs = new FileStream(@"c:\servicetest\filesystemwatcher.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("File Deleted\t" + e.FullPath.ToString() + "\t" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.Flush();
m_streamWriter.Close();
}

private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
{
FileStream fs = new FileStream(@"c:\servicetest\filesystemwatcher.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("Old File Name\t" + e.OldFullPath.ToString() + "\t" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.WriteLine("File Renaming \t" + e.FullPath.ToString() + "\t" + System.DateTime.Now.Date.ToShortDateString() + "--->" + System.DateTime.Now.Date.ToShortTimeString());
m_streamWriter.Flush();
m_streamWriter.Close();

}


Comments

Author: maharaja28 Jan 2009 Member Level: Bronze   Points : 0

Hi,

very useful for me.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: