NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
How Long For Me
SaveMySerials

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Autostarting a Windows Service directly after install in C#

Thursday, November 30th, 2006

So yesterday I was finishing up an IP Reporting program for my family seeing as we live all over the damn place, and with how mobile we all are, there are alot of times we need to remote into computers when we are somewhere else. We were using DynDns, but for somereason DynDns REALLY doesn't like Brasil or anywhere outside the US for that matter. So my dad would come back to the US on business and need to get into Brasil computer at home, and no luck. So I whipped up a windows service that reports to a webservice on my hosted servers, and all he has to do is go to a URL that I provided, and enter the identifer for whichever computer he wants to retrieve the IP Address. Pretty cool solution, but I'm going off on a tangent.

The fact of the matter is the majority of the work in this system is done by the windows service, which automatically updates the IP address if it changes. When you use a windows service installer you can define that the service automatically starts with windows, but after the install that doesn't automatically start the service. Basically the service won't start until you restart the computer, or you go into the service manager manually which I think I'm the only one in my family which even knows that that means. This can be a problem when my family has computer that won't be restarted for months at a time.

So I started looking for a solution, and the ones I found were pretty elaborate, using VBS scripting and creating a post-install action in the commit section, writing console apps that scan the windows services for one with that name and start it, etc. After doing some of that nastiness for a few hours I stopped and told myself there had to be an easier way to do this... and there was.

I looked deeper into the ServiceInstaller class and low and behold found a very easy solution:

Add a custom handler for the event collection after the install has completed (the install of the service):

this.AfterInstall += new InstallEventHandler(AfterInstallComplete);

AfterInstallComplete is a custom function:
private void AfterInstallComplete(object sender, InstallEventArgs e)
{
ServiceController control = new ServiceController("YourServicename");
if (control.Status != ServiceControllerStatus.Running)
{
control.Start();
}
}

And thats it... kind of makes me angry that I spent so much time on this and that's all I had to do. Hopefully this post will prevent some reader from having to go through all the madness I did, haha.

That's all for now folks!

Tags

CSharp

Related Blogs

Retrieving Text from Win32 SDK's GetLastError() in C#
Creating High Quality Images with C# and GDI
Creating an Organizational Chart using C#: Part 2
Awesome .NET 4: Named and Optional Parameters for Classes and Methods
Retrieving the SID of a user or group account using the Win32 SDK and C#

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers