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 ...
随机推荐
- 10、第十节课jq420151012
1.点击交替显示隐藏功能 点击交替执行的:fadeToggle(1000) , slideToggle() , toggle(1000); 2.点击单独执行 单独显示/隐藏:sho ...
- Eclipse 运行ant build.xml
在命令行cmd运行mvn clean install,ant compiler,提示上述信息,是因为 maven的这个插件要求jdk1.6,但是本地电脑环境变量jdk版本为1.7.将JAVA_HOME ...
- ARGB和RGB
ARGB 一种色彩模式,也就是RGB色彩模式附加上Alpha(透明度)通道,常见于32位位图的存储结构. ARGB---Alpha,Red,Green,Blue. Alpha-图像通道 如果图形卡具有 ...
- ASP.NET数据绑定控件简介
•数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...
- Android - Error parsing XML: unbound prefix
概述 这个问题,虽然看起来不是问题,但是如果不知道的人,还会花点时间,有的人甚至重新安装ADT. 我一开始还以为是排版的问题(Layout),因为初学,弄来弄去,最好还是到网上搜. 其实就不是什么问题 ...
- java Byte.toString 方法与String.ValueOf(Byte)效率比较
int times = 10000000; Byte[] li = new Byte[times]; for (int i = 0; i < times; i++) { li[i] = (byt ...
- ComboBox相关操作
取组合框文本示例: 1 void ShowDlgWage::OnCbnSelendokCombo1() { // TODO: 在此添加控件通知处理程序代码 CString str; int i; i ...
- C++ 约瑟夫环
约瑟夫环: 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周 ...
- JavaScript:Object.prototype.toString方法的原理
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...
- Centos 6.5 搭建php环境(nginx+mariadb+php7)
1.mariaDb vim /etc/yum.repos.d/MariaDB.repo [mariadb] name = MariaDB baseurl = http://yum.mariadb.or ...