【发布时间】:2018-03-19 19:45:59
【问题描述】:
我一直在阅读这篇文章,但我开始失去我剩下的几颗弹珠了..
我正在尝试在服务器上运行一项服务,以监视将通过 UNC 保存到其中的电子表格的更新。 \server01\mon(例如)
现在,服务可以工作了,如果监视器和保存文件的机器是同一台机器,例如,如果我使用服务器将文件复制到文件夹,它可以工作,如果我在我的 PC 上运行该服务并监视 UNC 并将文件复制到我 PC 上的 UNC,它可以工作。但如果您从 PC-> 服务器(或其他方式)更新文件并且我已将服务设置为作为可以访问所有内容的管理帐户运行(事实上目前,就像我一样)
理想情况下,我的服务会监控它的本地路径,PC 会通过 UNC 写入。但如果我必须监控 UNC,那就是这样。但是 atm,我似乎无法让一个识别另一个更新。
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()
{
}
}
}
【问题讨论】:
-
尝试以 '\' 结束 UNC 路径,例如\\server01\mon\
-
@stuartd nope - 不相关,我没有收到任何错误,它说它正在监视它,并且(如上所述)如果我使用打开服务的机器复制文件,它会看到文件..如果另一台机器更新文件,它没有看到它。
-
@SimplyGed Nope - 没有区别
标签: c# filesystemwatcher