[译]Windows服务更新程序
By robot-v1.0
本文链接 https://www.kyfws.com/applications/windows-service-updater-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 2 分钟阅读 - 601 个词 阅读量 0[译]Windows服务更新程序
原文地址:https://www.codeproject.com/Articles/15556/Windows-Service-Updater
原文作者:Bruce Bayley
译文由本站 robot-v1.0 翻译
前言
Simple way to update a running windows service
更新正在运行的Windows服务的简单方法
介绍(Introduction)
这个简单的代码显示了如何更新Windows服务,而无需执行手动启动/停止过程.(This simple code shows how to update a Windows service without having to go through the manual process of starting/stopping it.)
要使其正常工作,请将新服务.exe文件放在指定目录中,然后该服务本身将查看文件系统.一旦看到自己的更新,便会调用(To make it work you put your new service .exe file in a specified directory and then the service itself looks at the file system. Once it sees an update for itself, it then calls)**服务更新(serviceupdate.exe)**停止服务,替换可执行文件,然后重新启动(to stop the service, replace the executable, and then restart) 再次服务.当必须对服务进行大量更新时,这可以节省大量时间.(the service again. This saves plenty of time when having to do lots of updates to a service.)
这些进入Windows服务:(These go in the windows service:)
private void IntializeFileSystemWatcher()
{
System.IO.FileSystemWatcher newexe=new System.IO.FileSystemWatcher(
"c:\\services\\Updates","newservice.exe");
newexe.Created += new FileSystemEventHandler(NewExecutable);
newexe.EnableRaisingEvents = true;
}
private void NewExecutable(object source, FileSystemEventArgs e)
{
Process.Start("c:\\services\\ServiceUpdate.exe","newservice");
}
protected override void OnStart(string[] args)
{
IntializeFileSystemWatcher();
}
内容(Contents of)服务更新(serviceupdate.exe):(:)
static void Main(string[] args)
{
if (args.Length == 0)
return;
string filename=args[0];
System.ServiceProcess.ServiceController target = new
System.ServiceProcess.ServiceController(filename);
target.Stop();
while(target.Status !=
System.ServiceProcess.ServiceControllerStatus.Stopped)
{
target.Refresh();
}
Thread.Sleep(2000);
File.Delete("c:\\services\\Updates\\" + filename + ".exe");
File.Move("c:\\services\\Updates\\" + filename +
".exe","c:\\services\\" + filename + ".exe");
target.Start();
}
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Windows .NET .NET1.1 ASP.NET Visual-Studio WebForms VS.NET2003 Dev 新闻 翻译