无法打开本地计算机上的服务来进行写入,本地计算机上的Windows服务已启动,然后停止错误...

博主在将代码从Windows窗体应用移植到Windows服务时遇到问题,服务启动后立即停止。问题出在使用FileSystemWatcher监控文件夹时,只有当指定文件而非文件夹时服务才能正常启动。作者怀疑文件夹监控在服务中可能不工作,并且每次修改代码都需要创建新的服务进行测试。更新表明,服务对于特定文件夹路径的监控似乎存在不一致的行为。
摘要由CSDN通过智能技术生成

通常,我收到此错误:(本地计算机上的“服务名称”服务启动然后停止 . 如果我的代码出现问题,如某些服务未被其他服务或程序使用,则会自动停止驱动路径等.Windows服务无法启动 .

如果达到大小限制,我有一个备份文件夹/文件的Windows服务到一个位置 . 详细信息都是由Windows服务在启动时读取的XML配置提供的 . 我有一个单独的窗体,有一个按钮,完全符合我的Windows服务的启动 . 在将其放入Windows服务之前,我使用我的Windows窗体来调试代码 .

当我启动我的Windows窗体 . 它做了它想做的事情 . 当我把我的代码放在Windows服务OnStart()方法时出现错误 .

这是我的代码:

protected override void OnStart(string[] args)

{

private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";

private static string serviceStat = @"D:\LogBackupConfig\Status.txt";

private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";

protected override void OnStart(string[] args)

{

if (File.Exists(backupConfig))

{

FileSystemWatcher watcher = new FileSystemWatcher();

XmlTextReader reader = new XmlTextReader(backupConfig);

XmlNodeType type;

List listFile = new List();

string fileWatch = "";

//this loop is for reading XML elements and assigning to variables

while (reader.Read())

{

type = reader.NodeType;

if (type == XmlNodeType.Element)

{

if (reader.Name == "File")

{

reader.Read();

fileWatch = reader.Value;

}

else if (reader.Name == "Folder")

{

reader.Read();

fileWatch = reader.Value;

}

}

}

reader.Close();

watcher.Path = fileWatch;

watcher.Filter = "*.*";

//this loop reads whether the service will watch a file/folder

XmlTextReader reader1 = new XmlTextReader(backupConfig);

while (reader1.Read())

{

type = reader1.NodeType;

if (type == XmlNodeType.Element)

{

if (reader1.Name == "File")

{

watcher.IncludeSubdirectories = false;

watcher.Changed += new FileSystemEventHandler(OnChangedFile);

}

else if (reader1.Name == "Folder")

{

watcher.IncludeSubdirectories = true;

watcher.Changed += new FileSystemEventHandler(OnChangedFolder);

}

}

}

reader1.Close();

watcher.EnableRaisingEvents = true;

}

else

{

StreamWriter sw = new StreamWriter(serviceStat, true);

sw.WriteLine("File not found. Please start the Log Backup UI first.");

sw.Close();

}

}

我不知道是什么让Windows服务无法启动,Windows窗体模拟器工作正常 . 什么似乎是问题?

更新:经过多次试验,我注意到只使用文件夹目录(w / out文件),Windows服务不起作用 . 当我用特定文件(包括其目录)替换fileWatch变量时,Windows服务启动了 . 当我将其更改回文件夹位置时,它无法正常工作 . 我认为文件夹位置在filewatcher中不起作用 .

当我尝试创建一个监视文件夹位置的新Windows服务时,它工作..但是,当我在原始的Windows服务中尝试相同的位置时,它不起作用!我很精神!#ed ed!似乎每次我放置一个新的代码/函数时我都必须创建一个新的Windows服务并构建安装程序 . 这样我就可以跟踪我收到错误的位置 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值