写个windows服务想在机器启动的时候删除文件的问题

apipb0305 2006-11-02 11:57:23
写了后,在启动服务的时候提示: "本机器上的服务启动后又停止了,一些服务自动停止,如果他们没什么可做的,例如性能警报和日志",而且文件也没删除,麻烦解决下谢谢了,而且我想删除完文件后就停止服务怎么做?

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;

namespace zsglService
{
public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private EventLog log = new EventLog();

public Service1()
{
// 该调用是 Windows.Forms 组件设计器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化
}

// 进程的主入口点
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// 同一进程中可以运行多个用户服务。若要将
//另一个服务添加到此进程,请更改下行
// 以创建另一个服务对象。例如,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// 设置具体的操作,以便服务可以执行它的工作。
/// </summary>
protected override void OnStart(string[] args)
{
log.WriteEntry("servicStart");
Directory.Delete(@"C:\Inetpub\wwwroot\zsgl\Document\");
log.WriteEntry("deleteFileOk");
Directory.CreateDirectory(@"C:\Inetpub\wwwroot\zsgl\Document");
log.WriteEntry("createFileOk");
// this.OnStop();
}

/// <summary>
/// 停止此服务。
/// </summary>
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
log.WriteEntry("serviceStop");
}
}
}
...全文
111 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
snake290 2006-11-27
  • 打赏
  • 举报
回复
你打开服务,把你写的服务选中打开属性,把登录方式改为本地系统帐户.
bestshl 2006-11-02
  • 打赏
  • 举报
回复
看看你记的系统日志,删除文件夹处有没有错误
brucenan999 2006-11-02
  • 打赏
  • 举报
回复
我机子最近也是有很多SERVICE有这个问题,看来不是你程序的问题.看看系统的日志记录,在管理工具里,会有详细的出错信息.

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧