C# 创建window服务
using System;using System.Threading;
using System.ServiceProcess;
using System.Collections;
using System.Configuration.Install;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.IO;
namespace WSGPSServices
{
public partial class myService : ServiceBase
{
#region 成员变量
public string strCurrPath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory.FullName;
public string servicesname = "WSGPSServices";//服务
public string Proceename = "WSGPSGateway.exe";// 进程
#endregion
protected override void Dispose(bool disposeing)
{
if (disposeing)
{
if (components != null)
{
components.Dispose();
}
Register(servicesname, strCurrPath);
}
}
#region 注册卸载服务
//注册服务
public void Register(string servicesname, string strServiceInstallPath)
{
IDictionary mySavedState = new Hashtable();
try
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(servicesname);
//服务已经存在则卸载
if (ServiceIsExisted(servicesname))
{
UnInstallService(servicesname, strCurrPath);
}
service.Refresh();
//注册服务
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
mySavedState.Clear();
myAssemblyInstaller.Path = "WSGPSServices.exe";
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Install(mySavedState);
myAssemblyInstaller.Commit(mySavedState);
myAssemblyInstaller.Dispose();
service.Start();
ProcessStartInfo startinfo = new ProcessStartInfo("WSGPSGateway.exe");
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.Arguments = Application.StartupPath + "//WSGPSGateway.exe";
Process.Start(startinfo);
InitializeComponent();
}
catch (Exception ex)
{
throw new Exception("注册服务时出错:" + ex.Message);
}
}
//卸载服务
public void UnInstallService(string strServiceName, string strServiceInstallPath)
{
try
{
if (ServiceIsExisted(servicesname))
{
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = "WSGPSServices.exe";
myAssemblyInstaller.Uninstall(null);
 
补充:软件开发 , C# ,