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 ...
随机推荐
- 网页JavaScript3
window.document 1,确认元素, document.getElementById("id"); 根据id找元素 doucment.getEleme ...
- 使用三层交换机的ACL实现不同vlan间的隔离
使用三层交换机的ACL实现不同vlan间的隔离 建立三个vlan vlan10 vlan20 vlan30 www.2cto.com PC1 PC3属于vlan10 PC2 PC4属于v ...
- PHP Math
PHP Math 简介 Math 函数能处理 integer 和 float 范围内的值. 安装 PHP Math 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP 5 Math 函 ...
- mysql命中索引规律
就算where条件里写了and两个条件,然后建了2个独立索引,1个组合索引,他具体命中哪个是不确定的,他会根据数据分布情况选择. 比如 possible_keys :f1,f2,f1f2,有三种 ...
- trident教程
(一)理论基础更多理论以后再补充,或者参考书籍1.trident是什么?Trident is a high-level abstraction for doing realtime computi ...
- php判断是移动端还是pc
//判断是否是手机 function is_mobile() { $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_pc = (strpos( ...
- Css3 javascript 写的分类
不兼容IE10以下的浏览器 <!DOCTYPE html> <html> <head> <meta charset=utf-> <title> ...
- CFont 字体类
CFont 字体类 初始化函数 CreateFontIndirect 初始化一个由LOGFONT结构给出其特征的CFont对象 CreateFont 初始化用指定特性定义的CFont对象 Create ...
- java8-concurrency-tutorial-thread-executor-examples
http://ifeve.com/java8-concurrency-tutorial-thread-executor-examples/
- 我只能说,CDH5真的屌爆了!!!
参考URL http://blog.csdn.net/yangzhaohui168/article/details/34185579 http://blog.csdn.net/yangzhaohui1 ...