Windows服务未按预期工作-C#
我使用Windows服务未按预期工作-C#,c#,.net,service,windows-services,file-watcher,C#,.net,Service,Windows Services,File Watcher,我使用FileWatcherclass构建了一个windows服务。它必须检测文件夹中的传入文件并将其移动到新位置 我的代码作为控制台应用程序运行良好,但不能作为Windows服务运行。文件仅保留在源文件夹中。他们没有移动到目标。我已授予在本地系统帐户中运行的文件夹和服务的完全访问权限。你能指出错误吗。提前谢谢 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat
FileWatcher
class构建了一个windows服务。它必须检测文件夹中的传入文件并将其移动到新位置
我的代码作为控制台应用程序运行良好,但不能作为Windows服务运行。文件仅保留在源文件夹中。他们没有移动到目标。我已授予在本地系统帐户中运行的文件夹和服务的完全访问权限。你能指出错误吗。提前谢谢
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服务在LocalSystem帐户下运行(除非在安装时指定其他帐户)。您需要授予此帐户访问这两个文件夹的权限。使用以下命令创建具有用户名和密码的服务:
sc create "ServiceName" binPath= "G:\services\service.exe" DisplayName= "ServiceName" start= "auto" obj= "username" password= "password"
雷帕斯:
ServiceName、exe文件位置、DisplayName、用户名和密码。什么不起作用?您是否尝试调试windows服务?请提供有关什么不正常的更多详细信息。访问权限不足C:\Users\ADMIN maybe?苍蝇没有移动到目的地。该文件保持其在源代码中的状态folder@duDE. 我已授予该文件夹的完全访问权限。代码作为控制台应用程序运行良好。但不是windows service.Hi。我在本地系统下运行windows服务,并且对文件夹也有完全访问权限。您好。谢谢您的评论。是否有一种方法可以在没有用户ID和密码的情况下运行上述程序..?服务需要足够的权限来访问在此情况下无法访问的文件,因为文件夹位置与管理员用户的私有文件夹位置相同。您可以将文件位置移动到通用驱动器(C驱动器除外),也可以使用管理员用户名和密码。当您将其作为控制台应用程序运行时,它将使用您的用户名访问该文件,因此该文件在该时间起作用。