安装Winfows服务首先要添加安装程序,添加安装程序步骤如下:
1、将Windows服务程序切换到设计视图, 右击设计视图选择“添加安装程序”
一般设置如下:
设置serviceInstaller1组件的属性:
1) ServiceName = 服务名称
2) StartType = Automatic ,即自动
3)Description="服务的说明";
设置serviceProcessInstaller1组件的属性
1) Account = LocalSystem,账户一般设置为本地系统
3、生成解决方案
使用bat 方式安装和下载服务
在 ProjectInstaller 放入代码
String conServiceName = "DCCE";/// <summary>
/// 安装服务
/// </summary>
/// <param name="starname"></param>
public override void Install(System.Collections.IDictionary stateSaver)
{
Microsoft.Win32.RegistryKey system,
currentControlSet,
services,
service,
config;
try
{
//Let the project installer do its job
base.Install(stateSaver);
//Open the HKEY_LOCAL_MACHINE\SYSTEM key
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
//Open CurrentControlSet
currentControlSet = system.OpenSubKey("CurrentControlSet");
//Go to the services key
services = currentControlSet.OpenSubKey("Services");
//Open the key for your service, and allow writing
service = services.OpenSubKey(conServiceName, true);
//Add your service's description as a REG_SZ value named "Description"
service.SetValue("Description", "<span style=\"font-family:KaiTi_GB2312;\">描述语言</span>");
//(Optional) Add some custom information your service will use...
config = service.CreateSubKey("Parameters");
}
catch (Exception e)
{
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="savedState"></param>
public override void Uninstall(System.Collections.IDictionary savedState)
{
Microsoft.Win32.RegistryKey system,
currentControlSet,
services,
service;
try
{
//Drill down to the service key and open it with write permission
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
currentControlSet = system.OpenSubKey("CurrentControlSet");
services = currentControlSet.OpenSubKey("Services");
service = services.OpenSubKey(conServiceName, true);
//Delete any keys you created during installation (or that your service created)
service.DeleteSubKeyTree("Parameters");
//...
}
catch (Exception e)
{
Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
}
finally
{
//Let the project installer do its job
base.Uninstall(savedState);
}
}
代码添加完成后
添加window service安装的批处理命令
1)在项目添加一个文本文件,更名为install.bat,编辑文件的内容如下:
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe -i "WS.exe"
@pause
2)在项目添加一个文本文件,更名为uninstall.bat,编辑文件的内容如下
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe -u "WS.exe"
@pause
WS.exe 是生成exe文件。Framework 版本地址保持和服务版本一直
下边是服务具体工作
public partial class DCCservers : ServiceBase
{
public DCCservers()
{
InitializeComponent();
fileorder("运行");
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 1000 * 60 * Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeM"]);
t.Elapsed += new System.Timers.ElapsedEventHandler(RunWork);
t.AutoReset = true;//设置是执行一次(false),还是一直执行(true);
t.Enabled = true;//是否执行
}
//写日志
public void fileorder(string logorder, string wjj = "log")
{
string fname = Application.StartupPath + "\\" + wjj + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
FileStream LogFile = new FileStream(fname, FileMode.Append);
StreamWriter sw = new StreamWriter(LogFile);
sw.WriteLine(DateTime.Now + "\t" + logorder);
sw.Close();
}
protected override void OnStart(string[] args)
{
fileorder("开启");
}
public void RunWork(object source, System.Timers.ElapsedEventArgs e)
{
try
{
SqlHelp.GetDataNoPar(@"DBCC FREEPROCCACHE
DBCC freeproccache
DBCC FREESESSIONCACHE
DBCC FREESYSTEMCACHE('All')
");
fileorder("执行成功");
}
catch (System.Exception ex)
{
fileorder("执行失败:" + ex.ToString());
}
}
protected override void OnStop()
{
}
}
补充:
有得时候dos命令运行不是管理员,发现上面安装不正常,下面使用dos命令安装
以管理员身份 打开cmd
键入指令
1. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 执行
2. InstallUtil.exe U:\ceshiweb\Winserversc\bin\Debug\Winserversc.exe 执行
“U:\ceshiweb\Winserversc\bin\Debug\Winserversc.exe” 是我的程序exe路径
卸载服务
打开cmd 键入
1. cd / 执行
2. sc delete Winserversc 执行
语法 sc delete 服务名称