Hi All,
I test the FileSystemWatcher in Windows Service.OnStart event , begining Monitoring file incoming and OnStop event, stop monitor when
Folder has 3 Megabyte .
When running this code OnStop event not function.somebody show me the code.
Thank You..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace Win_Se1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var watcher = new FileSystemWatcher(@"C:\Users\family\Documents\Monitor");
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Created += OnCreated;
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
Console.WriteLine(value);
}
protected override void OnStop()
{
IsTextFileFull(@"C:\Users\family\Documents\Monitor");
}
public static bool IsTextFileFull(string fileName)
{
int Num_ = byte.MaxValue;
byte.MaxValue.ToString("3000000"); //3 MegaByte.
var info = new FileInfo(fileName);
if (info.Length < Num_)
{
var content = File.ReadAllText(fileName);
return content.Length == 0;
}
return true;
}
}
}