Windows服务不在Windows 8中执行OnStart()方法 [英] Windows service doesn't execute OnStart() method in Windows 8

查看:204
本文介绍了Windows服务不在Windows 8中执行OnStart()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我用C#开发了一个Windows服务,其启动类型为Automatic。 OnStart()方法在文本文件中写入一些值。我的服务在Windows 7中运行良好。但是当我在Windows 8中安装它时,OnStart()方法不会被执行(因为它不会在文本文件中写入值)。其他也在文本文件中写入值的服务方法也可以在Windows 8中完美地工作。任何人都可以帮我解决这个问题。下面是我的OnStart()方法代码。提前致谢。



Hello. I have developed a windows service in C#, whose startup type is Automatic. OnStart() method writes some values in a text file. My service works perfect in windows 7. But when I installed it in windows 8, the OnStart() method is not executed (as it does not write value in text file). Other methods of service which also write values in text file work perfectly in windows 8 also. Can anyone help me regarding this issue. Below is my OnStart() method code. Thanks in advance.

protected override void OnStart(string[] args)
{
	FileStream fs = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write);
	StreamWriter sw = new StreamWriter(fs);
	sw.BaseStream.Seek(0, SeekOrigin.End);
	sw.WriteLine("some value");
	sw.Flush();
	sw.Close();
}

推荐答案

您是否尝试过将IDE连接到流程然后测试应用程序?



在IDE中,从菜单中选择工具,然后选择附加到流程



选择您的服务并逐步执行代码。



首先使用Windows 8对表单应用程序中的函数进行测试。可能是服务确实启动但随后结束,因为抛出的异常是你不知道的。



您还可以在try catch语句的catch部分中显示一个消息框,但请记住将服务设置为允许与桌面通信,否则消息框功能也将被忽略。
HAve you tried attacing your IDE to the process and then Test the application?

In you IDE select Tools from the menu then "Attach to Process"

Select your service and step through your code.

Alternatviely test the function in a forms application first using windows 8. Chances are the service does start but then ends because of an exception being thrown that you are not aware of.

You can also display a message box in the catch portion of a try catch statement, but remember to set the service to allow communiction with the desktop otherwise the messagebox function will also be ignored.


这篇关于Windows服务不在Windows 8中执行OnStart()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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