Skip to content

Instantly share code, notes, and snippets.

@msell
Created August 29, 2012 04:54
Show Gist options
  • Save msell/3506961 to your computer and use it in GitHub Desktop.
Save msell/3506961 to your computer and use it in GitHub Desktop.
fun with windows services
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.ServiceProcess;
using System.Text;
using Microsoft.Win32;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace whereiswaldo
{
public static class WindowsServiceDohickey
{
public static string GetImagePath(string serviceName)
{
string registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
RegistryKey keyHKLM = Registry.LocalMachine;
RegistryKey key;
key = RegistryKey.OpenRemoteBaseKey
(RegistryHive.LocalMachine,Environment.MachineName).OpenSubKey(registryPath);
string value = key.GetValue("ImagePath").ToString();
key.Close();
return ExpandEnvironmentVariables(value);
}
public static string GetInstallationDirectory(string serviceName)
{
return Path.GetDirectoryName(GetImagePath(serviceName));
}
private static string ExpandEnvironmentVariables(string path)
{
string systemRootKey = @"Software\Microsoft\Windows NT\CurrentVersion\";
RegistryKey key = RegistryKey.OpenRemoteBaseKey
(RegistryHive.LocalMachine, Environment.MachineName).OpenSubKey(systemRootKey);
string expandedSystemRoot = key.GetValue("SystemRoot").ToString();
key.Close();
path = path.Replace("%SystemRoot%", expandedSystemRoot);
return path;
}
}
/// <summary>
/// Working with windows services
/// Stoping and starting has permissions issue
/// </summary>
///
///
[TestFixture]
public class describe_finding_the_location_of_a_windows_service
{
private const string _serviceName = "NetTcpPortSharing";
[Test]
public void get_service_path_from_registry()
{
Console.WriteLine(WindowsServiceDohickey.GetInstallationDirectory(_serviceName));
}
[Test]
public void does_service_exist()
{
Assert.True(ServiceController.GetServices().Any(serviceController => serviceController.ServiceName == _serviceName));
}
[Test]
public void start_the_service()
{
try
{
//
/***** this does not work becuase the app does not have the proper permission
var service = ServiceController.GetServices().FirstOrDefault(serviceController => serviceController.ServiceName == _serviceName);
service.Start();
*/
var service = ServiceController.GetServices().FirstOrDefault(serviceController => serviceController.ServiceName == _serviceName);
service.Start();
}
catch(Exception ex)
{
Console.WriteLine( ex.Message);
}
}
[Test]
public void where_is_the_service_installed()
{
try
{
var service = ServiceController.GetServices().FirstOrDefault(serviceController => serviceController.ServiceName == _serviceName);
Console.WriteLine(service.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
[Test]
public void stop_the_service()
{
try
{
var service = ServiceController.GetServices().FirstOrDefault(serviceController => serviceController.ServiceName == _serviceName);
service.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
[Test]
public void is_the_service_running()
{
try
{
var service = ServiceController.GetServices().FirstOrDefault(serviceController => serviceController.ServiceName == _serviceName);
Console.WriteLine(string.Format("Service is {0}", service.Status.ToString()));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment