【发布时间】:2017-10-21 22:19:53
【问题描述】:
我使用FileWatcher 类构建了一个Windows 服务。它必须检测文件夹中的传入文件并将它们移动到新位置。
我的代码作为控制台应用程序运行良好,但不能作为 Windows 服务运行。文件仅保留在 Source 文件夹中。他们没有移动到目的地。我已授予对在本地系统帐户中运行的文件夹和服务的完全访问权限。你能指出错误吗。提前致谢。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
bulk_watch();
}
public static void bulk_watch()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Users\ADMIN\Downloads\FW_Source";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
public static void OnChanged(object sender, FileSystemEventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(@"C:\Users\IBM_ADMIN\Downloads\FW_Source");
FileInfo[] files = directory.GetFiles("*.*");
foreach (var f in files)
{
File.Move(f.FullName, System.IO.Path.Combine(@"C:\Users\ADMIN\Downloads\FW_Dest", Path.GetFileName(f.FullName)));
}
}
protected override void OnStop()
{
}
}
}
【问题讨论】:
-
什么不起作用?您是否尝试过调试 Windows 服务?请提供更多关于不正常的细节。
-
C:\Users\ADMIN 权限不足?
-
苍蝇没有移动到目的地。该文件保留在源文件夹中
-
@duDE。我已授予对该文件夹的完全访问权限。该代码作为控制台应用程序运行良好。但不能作为 Windows 服务。
标签: c# .net service windows-services file-watcher