我已经创建并安装了C#FileSystemWatcher
,这部分工作但是当我将文件添加到源文件夹时没有任何反应,我添加到源文件夹的文件应该被复制到目标文件夹,但是没有发生。
这是我的Filesyswatcher.designer.cs
using System;
using System.Configuration;
using System.IO;
namespace HotFolderWatch
{
partial class FileSysWatcher
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.FSWatcher = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.FSWatcher)).BeginInit();
//
// FSWatcher
//
this.FSWatcher.Changed += new FileSystemEventHandler(FSWatcher_Changed);
this.FSWatcher.Created += new FileSystemEventHandler(FSWatcher_Created);
this.FSWatcher.Deleted += new FileSystemEventHandler(FSWatcher_Deleted);
this.FSWatcher.EnableRaisingEvents = true;
this.FSWatcher.NotifyFilter = ((System.IO.NotifyFilters)((((((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)
| System.IO.NotifyFilters.Size)
| System.IO.NotifyFilters.LastWrite)
| System.IO.NotifyFilters.LastAccess)
| System.IO.NotifyFilters.CreationTime)));
//
// FileSysWatcher
//
this.ServiceName = "FileSysWatcher";
((System.ComponentModel.ISupportInitialize)(this.FSWatcher)).EndInit();
}
#endregion
private System.IO.FileSystemWatcher FSWatcher;
/* DEFINE WATCHER EVENTS... */
/// <summary>
/// Event occurs when the contents of a File or Directory are changed
/// </summary>
private void FSWatcher_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
//code here for newly changed file or directory
}
/// <summary>
/// Event occurs when the a File or Directory is created
/// </summary>
private void FSWatcher_Created(object sender,
System.IO.FileSystemEventArgs e)
{
//code here for newly created file or directory
try
{
System.IO.File.Copy(e.FullPath, DestinationPath + e.Name, true);
}
catch (Exception ex)
{
//Util.WriteToErrorLogFile(ex);
}
}
/// <summary>
/// Event occurs when the a File or Directory is deleted
/// </summary>
private void FSWatcher_Deleted(object sender,
System.IO.FileSystemEventArgs e)
{
//code here for newly deleted file or directory
}
}
}
这是我的FileSysWatcher.cs
文件..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace HotFolderWatch
{
partial class FileSysWatcher : ServiceBase
{
private string _userName;
public FileSysWatcher()
{
InitializeComponent();
}
public static string DestinationPath;
public const string MyServiceName = "FileSysWatcher";
private FileSystemWatcher watcher = null;
protected override void OnStart(string[] args)
{
FSWatcher.Path = ConfigurationManager.AppSettings["WatchPath"];
DestinationPath = ConfigurationManager.AppSettings["DestinationPath"];
_userName = Environment.UserName;
// Begin watching.
FSWatcher.EnableRaisingEvents = true;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}
我还尝试在调试过程中附加到该过程,但事件似乎没有发生,任何人都会看到可能导致此错误的任何错误?
答案 0 :(得分:0)
我已经在我的系统上测试了您的代码(Windows 7 64):它可以运行。你应该检查一些事情:
并注释日志记录,因为该部分未执行。
好的,但是,你应该添加更多的日志记录。
我的Windows服务通常包含:
protected override void OnStart(string[] args)
{
// this is not just a simple message, this has to be called very early before any worker thread
// to prevent a race condition in the .NET code of registering the event source
EventLog.WriteEntry("XxxxService is starting", EventLogEntryType.Information, 1000);
答案 1 :(得分:0)
我最近遇到了这个问题,在上面的答案中找到了下面的代码,
FSWatcher.EnableRaisingEvents = true;
这行代码之前是在抛出异常,但是后来,在添加了上面这行之后,我的观察者正在触发事件,这是没有意义的。