Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#FileSystemWatcher Windows服务未启动_C#_Windows_Service - Fatal编程技术网

C#FileSystemWatcher Windows服务未启动

C#FileSystemWatcher Windows服务未启动,c#,windows,service,C#,Windows,Service,我已经创建并安装了C#FileSystemWatcher,此部分可以工作,但在向源文件夹添加文件时不会发生任何事情,添加到源文件夹的文件应该复制到目标文件夹,但不会发生这种情况 这是我的Filesyswatcher.designer.cs using System; using System.Configuration; using System.IO; namespace HotFolderWatch { partial class FileSysWatcher {

我已经创建并安装了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
        }


    }
}

我还尝试在调试时附加到进程,但事件似乎没有发生,有人看到任何可能导致此情况的错误吗?

我已在我的系统(Windows 7 64)上测试了您的代码:它可以工作。您应该检查以下几点:

  • WatchPath/DestinationPath是运行服务的系统上的本地路径吗?请注意,通过服务访问网络共享通常需要服务在(域)用户帐户下运行。此外,映射的网络驱动器未被服务看到,因此如果访问(SMB)文件共享,则应使用UNC路径
  • 在创建的事件触发时,文件可能正在使用中,因此您的复制可能会失败
日志被注释,因为部分也没有执行

好的,但无论如何,您应该添加更多日志记录

我的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);

我最近遇到了这个问题,在上面的答案中找到了下面的代码

FSWatcher.EnableRaisingEvents=true


这行代码以前抛出异常,但后来,在添加了这行代码之后,我的观察者触发了事件,这毫无意义。

服务启动了吗?启动后服务是否停止?是否发生任何异常?运行服务的用户是否对您正在监视的文件夹具有权限?为什么要注释掉异常处理程序中的日志代码?是的,服务启动,日志被注释,因为部分也没有执行…是的,服务可以停止,服务作为本地系统运行
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);