windows服务程序
首先创建一个myService的窗体程序作为服务安装卸载控制器(管理员身份运行vs,windows服务的安装卸载需要管理员权限)
在同一个解决方案里面添加一个windows服务程序,取名myWindowsService
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceTest()
};
ServiceBase.Run(ServicesToRun);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using System.Timers;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace myWindowsService
{
partial class ServiceTest : ServiceBase
{
public ServiceTest()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
System.Timers.Timer time = new System.Timers.Timer();
time.Interval = ;
time.Elapsed += new ElapsedEventHandler(WriteText);
time.AutoReset = true;
time.Enabled = true;
time.Start();
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
} private void WriteText(object source, ElapsedEventArgs es)
{
using (StreamWriter sw = File.AppendText(@"d:\test.txt"))
{
sw.WriteLine(DateTime.Now);
} } }
}
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe myWindowsService.exe
Net Start ServiceTest
sc config ServiceTest start= auto
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u myWindowsService.exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Configuration;
namespace myService
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btInstall_Click(object sender, EventArgs e)
{
string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
string CurrentDirectory = System.Environment.CurrentDirectory;
if (!ServiceIsExisted(ServiceName))
{
try
{
//新的线程,在10秒后判断服务是否存在,不存在则表示服务未安装成功
//Thread check = new Thread(new ThreadStart(CheckInstall));
//check.Start(); System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "Install.bat";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.ErrorDialog = true;
process.Start();
System.Environment.CurrentDirectory = CurrentDirectory;
label1.Text = ServiceName + "安装成功";
}
catch
{
System.Environment.CurrentDirectory = CurrentDirectory;
}
}
else
{
label1.Text = ServiceName + "已安装";
}
} private void btUninstall_Click(object sender, EventArgs e)
{
string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
string CurrentDirectory = System.Environment.CurrentDirectory;
if (ServiceIsExisted(ServiceName))
{
try
{
//Thread check = new Thread(new ThreadStart(CheckUninstall));
//check.Start(); System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "Uninstall.bat";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.ErrorDialog = true;
process.Start();
System.Environment.CurrentDirectory = CurrentDirectory;
label1.Text = ServiceName + "卸载成功";
}
catch
{
System.Environment.CurrentDirectory = CurrentDirectory;
}
}
else
{
label1.Text = ServiceName + "已卸载";
}
} private void btStart_Click(object sender, EventArgs e)
{
string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
try
{
ServiceController serviceController = new ServiceController(ServiceName);
if (!serviceController.CanStop)
serviceController.Start();
label1.Text = ServiceName + "启动成功";
}
catch
{
label1.Text = ServiceName + "未启动";
}
} private void btStop_Click(object sender, EventArgs e)
{
string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
try
{
ServiceController serviceController = new ServiceController(ServiceName);
if (serviceController.CanStop)
serviceController.Stop();
label1.Text = ServiceName + "停止成功";
}
catch
{
label1.Text = ServiceName + "未启动";
}
} /// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
private bool ServiceIsExisted(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
catch
{
return false;
}
} /// <summary>
/// 判断服务是否安装成功
/// </summary>
public void CheckInstall()
{
string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
Thread.Sleep();
if (!ServiceIsExisted(ServiceName))
MessageBox.Show("服务尚未安装成功,请确保以下三个名称一致," +
"\n1.程序的配置文件的服务名称 \n2.服务程序exe的名称 " +
"\n3.服务卸载脚本Uninstall.bat中服务程序名称 \n然后重新安装服务",
"提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 判断服务是否卸载成功
/// </summary>
public void CheckUninstall()
{
string ServiceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
Thread.Sleep();
if (ServiceIsExisted(ServiceName))
MessageBox.Show("服务尚未卸载成功,请确保以下三个名称一致," +
"\n1.程序的配置文件的服务名称 \n2.服务程序exe的名称 " +
"\n3.服务卸载脚本Uninstall.bat中服务程序名称 \n然后重新卸载服务",
"提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
}
<appSettings>
<add key="ServiceName" value="ServiceTest"/>
</appSettings>
windows服务程序的更多相关文章
- WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...
- C#写Windows Service(windows服务程序)
背景: 要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...
- 关于开发Windows服务程序容易搞混的地方!
在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...
- Windows服务程序和安装程序制作
转:http://www.cr173.com/html/15350_1.html 本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建 ...
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)
.net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...
- C# 编写Windows Service(windows服务程序)【转载】
[转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...
- C# 创建、安装和卸载Windows服务程序
1.新建一个windows服务程序. 2.点击这个服务类,从工具箱中加入一个Timer控件,右键这个Timer控件 命名为 timerOrderDeductionDetailJob,Enable设为T ...
- C语言编写Windows服务程序
原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...
- 创建一个Windows服务程序与实现定时器效果
1.创建一个Windows服务程序 一. 新建Window服务项目 二. 添加安装程序 三. 配置服务属性 四. 编写定时器代码 publicpartialclassService1 ...
随机推荐
- api接口
目录(?)[-] 接口特点汇总 PHP Token令牌 先说第一个tokenapi_token 服务端接口校验PHP实现流程如下 再说第二个tokenuser_token 接口用例如下 接口特点汇总: ...
- 组合控件 圆环 ring
使用 可以设置内部填充样式及大小 可以设置边框颜色及宽度 这里只是介绍了其中一种实现方式,其实这种类型的东西完全可以用自定义View去实现,他就是一个空中的大圆+一个空中或实心的小圆,实现起来也是非常 ...
- codevs 2800 送外卖(状压dp)
/* f[i][j] 表示走过的点构成i状态 且最后到达的点为j时的最优解 在那最后一个状态就是(1<<n+1)-1 每个点都到达 在由此回到0 */ #include<iostre ...
- HUD 2846 Repository
/* 开始想耍小聪明 直接map搞 代码短 好理解 空间够 恩 很好 就是 map慢死了 T了 */ #include<iostream> #include<cstdio> # ...
- Linux服务的管理
1.Linux服务的介绍 系统服务 --某些服务的服务的对象是Linux系统本身,或者Linux系统系统用户,这类服务我们称为系统服务(System Service) 网络服务 --提供给网络中的其他 ...
- Android布局文件-错误
View requires API level 14 (current min is 8): <?xml version="1.0" encoding="utf-8 ...
- 秒味课堂Angular js笔记------指令
1.属性指令 angularjs样式相关指令: ng-class ng-style ng-href ng-src ng-attr-(suffix) ng-bind ng-cloak 没解析完之前标签 ...
- ubuntu学习笔记--不断更新中
1.rpm软件包相关: rpm软件包安装命令: rpm -ivh linuxqq-v1.0.2-beta1.i386.rpm rpm软件默认安装路径查询: rpm -ql *.rpm ubuntu如何 ...
- JAVA为什么会空指针异常
1.所谓的指针,就是java中的对象的引用.比如String s;这个s就是指针. 2.所谓的空指针,就是指针的内容为空,比如上面的s,如果令它指向null,就是空指针. 3.所谓的空指针异常,就是一 ...
- 1104解决ecos挂件中数组传递的相关问题。
1.挂件综述: 挂件组成:_config.html 后台配置,即点添加时的弹出框. default.html 前台显示,即在前台显示出来的页面. widget.php 设置挂件的基本信息.. ...