FileSystemWatcher 不响应远程更新
FileSystemWatcher not responding to remote updates
我一直在读这个,我开始失去我剩下的几颗弹珠..
我正在尝试 运行 服务器上的一项服务,该服务监视将通过 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()
{
}
}
}
好的
原来我并不像我想的那么傻但是
将项目从 Outlook 保存到本地 PC 时,它只是保存到 c:\temp\filename.xls
当保存到 UNC 时,它会执行此操作(即使您说只监视 xlsx,也会出现 tmp 文件!!)
Created: 1A50796C.tmp
Changed: 1A50796C.tmp
Changed: 1A50796C.tmp
Renamed: 4203327D.tmp
Renamed: filename.xlsx
Deleted: 4203327D.tmp
所以..
简而言之,我必须添加一个检查文件扩展名是否为 .xlsx 并监听重命名.. 以便它在所有场景中工作。
我一直在读这个,我开始失去我剩下的几颗弹珠..
我正在尝试 运行 服务器上的一项服务,该服务监视将通过 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()
{
}
}
}
好的
原来我并不像我想的那么傻但是
将项目从 Outlook 保存到本地 PC 时,它只是保存到 c:\temp\filename.xls
当保存到 UNC 时,它会执行此操作(即使您说只监视 xlsx,也会出现 tmp 文件!!)
Created: 1A50796C.tmp
Changed: 1A50796C.tmp
Changed: 1A50796C.tmp
Renamed: 4203327D.tmp
Renamed: filename.xlsx
Deleted: 4203327D.tmp
所以..
简而言之,我必须添加一个检查文件扩展名是否为 .xlsx 并监听重命名.. 以便它在所有场景中工作。