Ive been reading up on this and Im beginning to lose the few marbles I have left..
Im trying to run a service on a server that monitors for the update of a spreadsheet that will get saved to it via UNC. \server01\mon (for example)
Now, the service works, if the monitor and the machine that saves the file are the same machine, eg, if I copy the file to the folder using the server, it works, if I run the service on my PC and monitor the UNC and copy the file to the UNC on my PC, it works.. But not if you update the file from PC-> server (or other way round) and I have set the service to run as an administrative account that has access to everything (in fact currently, as me)
Ideally, my service monitors its local path, a PC would write via UNC.. but if I have to monitor the UNC, then that will be that. But atm, I cant seem to get one to recognise the other updated.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
namespace watchtest
{
public partial class Service1 : ServiceBase
{
private FileSystemWatcher fsw = null;
private EventLog log;
private String path = @"c:\temp";
public Service1()
{
InitializeComponent();
((ISupportInitialize)this.EventLog).BeginInit();
if (!EventLog.SourceExists(this.ServiceName))
{
EventLog.CreateEventSource(this.ServiceName, "Application");
}
((ISupportInitialize)this.EventLog).EndInit();
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";
log = this.EventLog;
}
protected override void OnStart(string[] args)
{
this.EventLog.WriteEntry($"Monitoring {path}", EventLogEntryType.Information);
fsw = new FileSystemWatcher(path);
fsw.Filter = "*.xlsx";
fsw.Changed += new FileSystemEventHandler(f_Changed);
fsw.Created += new FileSystemEventHandler(f_Changed);
fsw.EnableRaisingEvents = true;
}
private void f_Changed(object sender, FileSystemEventArgs e)
{
this.EventLog.WriteEntry($"Changed {e.Name}", EventLogEntryType.Information);
}
protected override void OnStop()
{
}
}
}
Ok
turned out Im not as daft as I thought but
When saving the item from outlook to local PC it just saves to c:\temp\filename.xls
When saving to UNC it does this (and the tmp file appears even when you said only monitor for xlsx!!)
Created: 1A50796C.tmp
Changed: 1A50796C.tmp
Changed: 1A50796C.tmp
Renamed: 4203327D.tmp
Renamed: filename.xlsx
Deleted: 4203327D.tmp
So..
In short, I had to add a check that the file extension was .xlsx and listen for renamed.. for it to work in all senarios.