1.VS-》文件-》新建项目,弹出窗口中选择Visual C#-》windows-》windows服务
2.在新建的工程中,点击Service1.cs文件,切换到代码视图:
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.IO;
using System.Timers;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
using System.Net.Mail;
namespace TimedTask
{
public partial class Service1 : ServiceBase
{
Timer timer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer = new Timer(60000);//1000=1s,此处为1分钟
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer.Dispose();
}
//定时执行的任务
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
string time = DateTime.Now.AddDays(1).ToShortDateString() + " " + DateTime.Now.ToLongTimeString().ToString();
string filePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
StreamWriter sw = null;
if (!File.Exists(filePath))
{
sw = File.CreateText(filePath);
}
else
{
sw = File.AppendText(filePath);
}
sw.Write("访问时间:" + time + Environment.NewLine);
sw.Close();
}
}
}
3.生成工程,在bin目录下会生成exe文件
4.向系统添加该服务:
(1).找到系统盘下的.net路径:本机是C:\Windows\Microsoft.NET\Framework\v4.0.30319,找到其中的InstallUtil.exe
(2).找到工程文件路径下的bin\Debug\项目名称.exe
(3).打开命令行工具,输入:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe空格D:\WebSites\TimedTask\bin\Debug\项目名称.exe
执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。
注意:中文字符空格是表示敲击空格键。
5.启动该服务即可。
6.调试:
服务已启动时,在VS中选择调试-》附加到进程,可找到该服务名称,附加即可调试。
分享到:
相关推荐
C#创建Windows服务(Windows Services) 实战之系统定时重启服务-程序开发
C#创建Windows服务(Windows Services) 实战之系统定时重启服务
Asp.net(C#) 创建windows服务并定时执行某个接口,webservices等
要在具有大量现有 NIS 用户的环境中成功实现 Microsoft:registered: Windows:registered: Services for UNIX 3.5 (SFU 3.5),您同样需要创建 Windows:registered: 环境中的用户。如果现有的 NIS 服务器要继续为 UNIX...
用C#创建Windows服务(WindowsServices).pdf
C# WindowsServices实例下载,C# WindowsServices实例下载
该资源是一个用VS2008创建的Windows Services服务案例!主要包括定时生成一些时间信息 Windows Services服务,主要对什么是Windows Services,以及如何创建Windows Services,以及其中的一些问题进行呢解说! 详细...
创建创建Windows服务有3种方式: a.利用.net框架类ServiceBase b.利用组件Topshelf c.利用小工具instsrv和srvany 里面有详细说明和代码送上
Windows Media Services 流媒体服务器架设教程
windows sharepoint services 3.0,非常好的材料, 新出的
在单台服务器计算机上或在服务器场环境中安装 Microsoft Windows SharePoint Services 3.0 之后,您需要...本指南的末尾还提供到其他信息的链接,这些信息有助于您计划和实现 Windows SharePoint Services 3.0 部署。
Windows SharePoint Services办公应用入门
因为 Microsoft Windows 拥有健壮的体系结构,能够利用商用 Intel 服务器,并且拥有一套迁移与互操作工具,所以,对于那些打算将 UNIX 应用程序迁移到更新、成本效益更高的平台的企业来说,它无疑是一个诱人的选择。...
WindowsService定时触发事件,默认是触发webservices
Windows Media Services集成开发技术,更多免费资源下载:http://download.csdn.net/user/gouyue
Windows SharePoint Services 3.0 SDK 包括Windows SharePoint Services 3.0 软件开发工具包 (SDK) 的“通用参考”和 类库信息。
Microsoft Windows ServerUpdate Services
Introduction Microsoft Windows SharePoint Services
windows7 services端口表