Click here to Skip to main content
Click here to Skip to main content

Windows Service Updater

, 13 Mar 2007
Rate this:
Please Sign up or sign in to vote.
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:

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

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

Share

About the Author

Bruce Bayley

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralA little off topic. Pinmembersmesser1-Apr-07 19:02 
GeneralDoes the Trick PinmemberOwen Hines26-Mar-07 5:18 
GeneralSend email through window service Pinmembervidya_rpl16-Mar-07 7:19 
GeneralInnovation PinmemberBehind The Scene12-Sep-06 8:17 
GeneralRe: Innovation PinmemberAnthony Rudovsky12-Sep-06 14:15 
GeneralRe: Innovation Pinmembervik2012-Sep-06 20:59 
GeneralRe: Innovation Pinmembersteves013-Sep-06 0:09 
GeneralRe: Innovation PinmemberPepato30-Mar-07 0:20 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 13 Mar 2007
Article Copyright 2006 by Bruce Bayley
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid