Help Cannot raise events in my web service

Ninnad Jagtap

Active member
Joined
Apr 12, 2005
Messages
35
Programming Experience
Beginner
Writing Service for moving files in folder service is starting but cannot raise event heres my code can any 1 suggest how do i raise events... its urgent .. plz.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
namespace FAXMgmt
{
public class FAXMgmtService : System.ServiceProcess.ServiceBase
{
private System.IO.FileSystemWatcher fwService;
/// <summary>
/// Required designer variable.
/// </summary>
private string Pathin="C:\\FAX FOLDER";
private string Pathout="D:\\FAX SERVER FOLDER";
private System.ComponentModel.Container components = null;
public Exception ex;
public FAXMgmtService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new FAXMgmtService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.fwService = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(
this.fwService)).BeginInit();
//
// fwService
//
this.fwService.EnableRaisingEvents = true;
this.fwService.Deleted += new System.IO.FileSystemEventHandler(this.fwService_Deleted);
this.fwService.Renamed += new System.IO.RenamedEventHandler(this.fwService_Renamed);
this.fwService.Changed += new System.IO.FileSystemEventHandler(this.fwService_Changed);
this.fwService.Created += new System.IO.FileSystemEventHandler(this.fwService_Created);
//
// FAXMgmtService
//
this.ServiceName = "FAXMgmtService";
((System.ComponentModel.ISupportInitialize)(
this.fwService)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
System.IO.DirectoryInfo di;

try
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","In the Start " + Pathin);
di=
new System.IO.DirectoryInfo(Pathin);
if (! di.Exists)
{
di.Create();
}
try
{
di=
new System.IO.DirectoryInfo(Pathout);
if(!di.Exists)
{
di.Create();
}
try
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","FAX SERVICE Starting.1");
fwService =
new System.IO.FileSystemWatcher();

fwService.IncludeSubdirectories =
false;
//this.fwService.Filter="*.*";
fwService.EnableRaisingEvents = true;
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","FAX SERVICE Started Successfully.");
}
catch
{
try
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","FAX SERVICE Starting.2");
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Unable to watch the directory:" + Pathin + "" + System.Environment.NewLine + "Error Description " + ex.Message);
}
}
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Unable to create the directory:" + Pathin + "" + System.Environment.NewLine + "Error Description " + ex.Message);
}
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Unable to create the directory:" + Pathin + "" + System.Environment.NewLine + "Error Description " + ex.Message);
}
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","End of Start Start");
//fwService.EnableRaisingEvents = true;
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
try
{
fwService.Dispose();
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","FAX SERVICE Stopped.");
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Error in Stopping the Directory Watcher for Path :" + Pathin + System.Environment.NewLine + "Error Description " + ex.Message);
}
}
protected override void OnPause()
{
base.OnPause ();
try
{
this.fwService.EnableRaisingEvents=false;
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","FAX SERVICE Paused For Path." + Pathin);
}
catch
{
//StructuredErrorHandler(ex);
}
}
protected override void OnContinue()
{
base.OnContinue ();
fwService.EnableRaisingEvents=
true;
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","FAX SERVICE Paused For Path." + Pathin);
}
private void fwService_Created(object sender, System.IO.FileSystemEventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","In Created Function");
System.IO.FileInfo fi =
new System.IO.FileInfo(e.FullPath);
System.IO.StreamWriter sw;

try
{
fi.MoveTo(Pathout + "\\" + e.Name);
try
{
fi=
new System.IO.FileInfo(Pathout + "\\report.txt");
if (! fi.Exists)
{
sw=fi.CreateText();
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","In Created Function fi.Exits");
}
else
{
sw=fi.AppendText();
}
try
{
sw.WriteLine(e.FullPath + "," + Pathout + "\\" + e.Name);
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Unable to write to the file." + fi.FullName);
}
finally
{
try
{
sw.Close();
}
finally
{}
}
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Unable to create or append to the file " + Pathout + "\\" + e.Name);
}
}
catch
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","Unable to move the file" + e.FullPath + "\\" + e.Name);
}
}
private void fwService_Changed(object sender, System.IO.FileSystemEventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","In Changed");
}
private void fwService_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","In Delete");
}
private void fwService_Renamed(object sender, System.IO.RenamedEventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("C#NetWeb.DirectoryWatcher","In Rename");
}
}
}
 
Top Bottom