找不到文件时不应启动服务 [英] Service should not be started when the file not found

查看:84
本文介绍了找不到文件时不应启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在Windows服务.此服务设置为自动.在这项服务中,我检查了在C盘上任何地方创建的文件(我在服务中提供了此路径).

在启动服务时,我检查了给定的文件;如果找不到文件,则不应启动计时服务.

OnStop()中,我调用了另一个函数.(如果发现服务器路径,则单击停止"选项时,此函数用于断开服务器(如OPC服务器)的连接.
我在OnStart()
中使用了以下方法

Hi All,
I am working a windows service. This service set on automatic. In this service I checked the File which is created on any where like on C drive (I give this path in service).

At the time of start the service I check the given file; if file is not found that time service should not be started.

In OnStop() I call the one other function.(this function is used to disconnected my server (like OPC server) when click on stop option if the server path is found.

I used the following method in OnStart()

int exitCode = 0;
System.Environment.Exit(exitCode);


使用上述方法的服务不应启动.但是我收到一条错误消息


Using the above method service should not started. But I got an error message

"Error 1067: The process terminated unexpectedly."

如何处理此消息或更改错误消息找不到文件"?

请帮帮我.

How can I handle this message or change the error message "File not found"?

Please help me.

Thanks in Advance.

推荐答案

您可以使用File.Exists("path")来检查是否存在,然后进行编码.
You can use File.Exists("path") to check if it is there or not, Then proceed accodingingly.


如果要在OnStart中终止Windows服务并显示错误消息,请抛出未处理的异常:
If you want to terminate the windows service with an error message within OnStart, then throw an unhandled exception:
throw new ApplicationException("File not found: " + strFilename);


这篇关于找不到文件时不应启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆