First the code:

public partial class Watcher : ServiceBase
{
    private const string PathToFolder = @"D:\print\";

    public Watcher()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }

    private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
    {
        if (e.Name != "test.txt")
        {
            return;
        }
        using (var r = new StreamReader(e.FullPath))
        {
            var json = r.ReadToEnd();
            dynamic tempTest = JsonConvert.DeserializeObject(json);
            const string filename = PathToFolder + "textParsed.txt";
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            using (var file = File.CreateText(filename))
            {
                file.WriteLine(tempTest.Name.ToString());
            }
        }
    }
}

If there are changes to the text.txt file I'm suppose to parse the content of that text file and create another file. If I attach VS to the service and debug the service, the event gets fired, but when running normally, nothing happens.

The installer has LocalSystem privileges and that's pretty much all the changes I've made... Should be pretty straight forward, but somehow isn't.

Related posts

Recent Viewed