Click here to Skip to main content
Licence 
First Posted 12 Sep 2006
Views 33,229
Bookmarked 42 times

Windows Service Updater

By | 13 Mar 2007 | Article
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

About the Author

Anthony Rudovsky



United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA little off topic. Pinmembersmesser18:02 1 Apr '07  
GeneralDoes the Trick PinmemberOwen Hines4:18 26 Mar '07  
GeneralSend email through window service Pinmembervidya_rpl6:19 16 Mar '07  
GeneralInnovation PinmemberBehind The Scene7:17 12 Sep '06  
GeneralRe: Innovation PinmemberAnthony Rudovsky13:15 12 Sep '06  
GeneralRe: Innovation Pinmembervik2019:59 12 Sep '06  
GeneralRe: Innovation Pinmembersteves023:09 12 Sep '06  
GeneralRe: Innovation PinmemberPepato23:20 29 Mar '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Mar 2007
Article Copyright 2006 by Anthony Rudovsky
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid