项目告一段落,快到一年时间开发了两个系统,一个客户已经在试用,一个进入客户测试阶段,中间突然被项目经理(更喜欢叫他W工)分派一个每隔两小时用windows服务去抓取客户提供的外网 <??><list> <info> <id>9 ...
项目告一段落,快到一年时间开发了两个系统,一个客户已经在试用,一个进入客户测试阶段,中间突然被项目经理(更喜欢叫他W工)分派一个每隔两小时用get='_blank'>windows服务去抓取客户提供的外网
<??><list> <info> <id>93ef7c7cccd2ecb13ed01dd6e30020b4136</id> <title>剔骨刀一握六年练过硬本领 女庖丁解牛割肉有拿手好戏</title> <posttime class="sql-timestamp">2015-05-25 06:38:20.0</posttime> <institution>浙江省物价局</institution> <url>http://dsb.66wz.com/html/2015-05/25/content_1822593.htm</url> <webName>温州都市报数字版</webName> <infotype>新闻</infotype> <summary>内容</summary> <fl>其他类</fl> </info> <info> ... </info> <info> ... </info>。。。</list>
C:\Users\ld>cd C:\
Windows\Microsoft
.NET\Framework\v4.0.30319\InstallUtil.exeC:\Users\ld>cd C:\Windows\Microsoft.NET\Framework\v4.0.30319C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil D:\gawking\projects\Ae正在开始安装的“安装”阶段。“安装”阶段已成功完成,正在开始“提交”阶段。“提交”阶段已成功完成。已完成事务处理安装。C:\Windows\Microsoft.NET\Framework\v4.0.30319>
3.可以建两个服务启动和中止的批处理文件用来启动和中止服务:
或者新建一个Winform程序在界面按钮操作中实现开启和关闭服务的功能:
Form1.cs
using System;using System.Collections.Generic;using System.ComponentModel;using System.Configuration.Install;using System.Data;using System.Diagnostics;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Windows.Forms;using System.ServiceProcess;namespace AeGISSMSInfoForms{ public partial class Form1 : Form { //建立服务对象 private ServiceController sc = new ServiceController("AeGISSMSInfoService"); public Form1() { InitializeComponent(); } private void btnStartOrStop_Click(object sender, EventArgs e) { bool IsStart = false; //服务运行则停止服务 if (sc.Status.Equals(ServiceControllerStatus.Running)) { this.btnStart.Text = "启动服务"; sc.Stop(); WriteLog(string.Format( " \r\n" + "=====================================→停止服务成功 {0}←=====================================\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); MessageBox.Show("服务停止成功!"); } //服务停止则启动服务 if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending))) { this.btnStart.Text = "停止服务"; sc.Start(); WriteLog(string.Format( " \r\n" + "=====================================→启动服务成功 {0}←=====================================\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); MessageBox.Show("服务启动成功!"); } ////服务停止则启动服务 //if (this.btnStart.Text == "启动服务") //{ // this.btnStart.Text = "停止服务"; // sc.Start(); // WriteLog(string.Format( // " \r\n" + // "=====================================→启动服务成功 {0}←=====================================\r\n", // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); //} ////服务运行则停止服务 //else if (this.btnStart.Text == "停止服务") //{ // this.btnStart.Text = "启动服务"; // sc.Stop(); // WriteLog(string.Format( // " \r\n" + // "=====================================→停止服务成功 {0}←=====================================\r\n", // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); //} sc.Refresh(); } private void btnUninstall_Click(object sender, EventArgs e) { //ProcessStartInfo a = // new ProcessStartInfo( // string.Format(@"{0}\\AeGISSMSInfo.exe", Path.Combine(AppDomain.CurrentDomain.BaseDirectory)), // "-remove"); //a.WindowStyle = ProcessWindowStyle.Hidden; //a.UseShellExecute = false; //WriteLog(string.Format( // " \r\n" + // "=====================================→卸载服务成功 {0}←=====================================\r\n", // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); ////MessageBox.Show("服务卸载成功!"); ////Process process = Process.Start(a); ////UnInstallService(); //string pathLogs = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); //System.Diagnostics.Process.Start(pathLogs + "\\卸载该服务程序.exe"); if (ServiceIsExisted(sc.ServiceName)) { try { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; ManagedInstallerClass.InstallHelper(new string[] { "/u", "AeGISSMSInfo.exe" }); System.Environment.CurrentDirectory = CurrentDirectory; WriteLog(string.Format( " \r\n" + "=====================================→服务卸载成功 {0}←=====================================\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); MessageBox.Show("服务卸载成功!"); } catch (Exception ex) { MessageBox.Show("服务卸载出错:" + ex.Message); } } else { MessageBox.Show("您要卸载的服务不存在!"); } } private bool ServiceIsExisted(string svcName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == svcName) { return true; } } return false; } /// <summary> /// 写入日志 /// </summary> /// <param name="logMsg"></param> public void WriteLog(string logMsg) { string pathLogs = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"); string path = string.Format(pathLogs + "\\AeGISSMSInfoLog{0}.txt", DateTime.Now.ToString("yyyy-MM-dd")); if (!Directory.Exists(pathLogs)) Directory.CreateDirectory(pathLogs); if (!File.Exists(path)) File.Create(path); FileInfo finfo = new FileInfo(path); if (finfo.Exists) { using (FileStream fs = finfo.OpenWrite()) { //根据上面创建的文件流创建写数据流 StreamWriter w = new StreamWriter(fs); //设置写数据流的起始位置为文件流的末尾 w.BaseStream.Seek(0, SeekOrigin.End); w.Write(logMsg); //清空缓冲区内容,并把缓冲区内容写入基础流 w.Flush(); //关闭写数据流 w.Close(); } } } }}
原标题:我也来学着写写WINDOWS服务
关键词:Windows
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。