Share via

Byte in Folder

MiPakTeh 1,476 Reputation points
2021-12-26T05:17:42.427+00:00

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;
        }

    }
}
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2021-12-26T08:02:59.803+00:00

    Maybe this will work:

    private void OnCreated(object sender, FileSystemEventArgs e)
    {
       string value = $"Created: {e.FullPath}";
       Console.WriteLine(value);
    
       if( IsTextFileFull(@"C:\Users\family\Documents\Monitor") ) Stop( );
    }
    
    public bool IsTextFileFull(string fileName)
    {
       var info = new FileInfo(fileName);
    
       return info.Exists( ) && info.Length > 3_000_000; // (or 3 * 1024 * 1024)
    }
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.