Click here to Skip to main content
15,891,431 members
Articles / Web Development / ASP.NET
Article

Windows Service Updater

Rate me:
Please Sign up or sign in to vote.
2.73/5 (16 votes)
13 Mar 2007 52.1K   42   8
Simple way to update a running windows service

Introduction

This simple code shows how to update a Windows service without having to go through the manual process of starting/stopping it.

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.

These go in the windows service:

C#
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:

C#
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();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralA little off topic. Pin
Steve Messer1-Apr-07 18:02
Steve Messer1-Apr-07 18:02 
GeneralDoes the Trick Pin
Owen Hines26-Mar-07 4:18
Owen Hines26-Mar-07 4:18 
GeneralSend email through window service Pin
vidya_rpl16-Mar-07 6:19
vidya_rpl16-Mar-07 6:19 
GeneralInnovation Pin
Ri Qen-Sin12-Sep-06 7:17
Ri Qen-Sin12-Sep-06 7:17 
GeneralRe: Innovation Pin
Bruce Bayley12-Sep-06 13:15
Bruce Bayley12-Sep-06 13:15 
GeneralRe: Innovation Pin
vik2012-Sep-06 19:59
vik2012-Sep-06 19:59 
GeneralRe: Innovation Pin
SteWooHoo12-Sep-06 23:09
SteWooHoo12-Sep-06 23:09 
GeneralRe: Innovation Pin
Kwasir29-Mar-07 23:20
Kwasir29-Mar-07 23:20 
Well done. Usefull for me, thx
Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.