【问题标题】:FileSystemWatcher not responding to remote updatesFileSystemWatcher 不响应远程更新
【发布时间】: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


【解决方案1】:

好的

原来我没有我想的那么愚蠢,但是

将项目从 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 并监听重命名的.. 以便它在所有场景中工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多