Active Directory Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail
How Long For Me
<< 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)
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!
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#
Currently no comments.
Add A Comment
Email Address: (not public, used to send notifications on further comments)
Enter the text above, except for the 1st and last character: