0

I am learning .net and writing a service by following the tutorial below http://www.beansoftware.com/NET-Tutorials/Create-Windows-Services.aspx

The windows service gets installed successfully and it starts too. But when I change the file name or delete a file or add a file, it doesnt add any information to my log file which is in C:\Folder\FolderLog.txt

I see 3 methods for FolderWatcher_** but I dont see a call to those in the code. I am very new to .net so I am not sure how they are being called

here is my complete code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

    namespace Watcher
    {
        public partial class Watcher : ServiceBase
        {
            public Watcher()
            {
                InitializeComponent();
            }

            protected override void OnStart(string[] args)
            {

            }

            protected override void OnStop()
            {
            }

            private void FolderWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
            {
                TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true);
                writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been created. ");
                writer.Close();
            }

            private void FolderWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
            {
                TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true);
                writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been deleted. ");
                writer.Close();
            }

            private void FolderWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
            {
                TextWriter writer = new StreamWriter("C:\\folder\\log.txt", true);
                writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been renamed. ");
                writer.Close();
            }


        }
    }

I have followed every single step in that tutorial. I have not seen anything that descibes how these methods are called. Any help would be great

Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • Maybe, and I'm not sure of it because I never created a service, there could be permission issues. Windows 7 expecially is a bit strict (is better) about permission, you can't write on some paths if you aren't administrator. Hope this help in some way – Francesco Belladonna Aug 18 '11 at 00:13
  • Did you follow step 6 in the tutorial - hook the designer-created FolderWatcher to the event handlers shown above? – Michael Petrotta Aug 18 '11 at 00:14

2 Answers2

3

The tutorial probably has you adding a FileSystemWatcher in the design mode surface for your service. In design mode, select that object and in its event handlers, select the appropriate methods of your class.

Jacob
  • 77,566
  • 24
  • 149
  • 228
1

Make sure you register the events of the watcher instance "FolderWatcher" to the events handlers in your code, you can do that at service designer or in the constructor of the service, like:

public Watcher()
{
    InitializeComponent();

    FolderWatcher.Created += FolderWatcher_Created;//when created FolderWatcher_Created will be called
    FolderWatcher.Deleted += FolderWatcher_Deleted;//when deleted call FolderWatcher_Deleted will be called
    FolderWatcher.Renamed += FolderWatcher_Renamed;//when renamed FolderWatcher_Renamed will be called
}
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • I have a similar question here http://stackoverflow.com/questions/7101326/running-a-batch-file-from-c may be you can help there too. thanks – Asim Zaidi Aug 18 '11 at 01:13