Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ctl = ServiceController.GetServices().Where(s => s.ServiceName == "MyService").FirstOrDefault();
- if (ctl != null) {
- // now what?
- }
- private static string GetExecutablePathForService(string serviceName, RegistryView registryView, bool throwErrorIfNonExisting)
- {
- string registryPath = @"SYSTEMCurrentControlSetServices" + serviceName;
- RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(registryPath);
- if(key==null)
- {
- if (throwErrorIfNonExisting)
- throw new ArgumentException("Non-existent service: " + serviceName, "serviceName");
- else
- return null;
- }
- string value = key.GetValue("ImagePath").ToString();
- key.Close();
- if(value.StartsWith("""))
- {
- value = Regex.Match(value, ""([^"]+)"").Groups[1].Value;
- }
- return Environment.ExpandEnvironmentVariables(value);
- }
- var ctl = ServiceController
- .GetServices()
- .FirstOrDefault(s => s.ServiceName == "MyService");
- if (ctl != null) {
- // get version substring, you might have your own style.
- string substr = s.DisplayName.SubString("MyService".Length);
- Version installedVersion = new Version(substr);
- // do stuff, e.g. check if installed version is newer than current assembly.
- }
- public static string ServiceVersion { get; private set; }
- ServiceVersion = typeof(Program).Assembly.GetName().Version.ToString();
- using System.Diagnostics;
- using System.ServiceProcess;
- public partial class VaultServerUtilities : ServiceBase
- {
- public static string ServiceVersion { get; private set; }
- public VaultServerUtilities()
- {
- InitializeComponent();
- VSUEventLog = new EventLog();
- if (!EventLog.SourceExists("Vault Server Utilities"))
- {
- EventLog.CreateEventSource("Vault Server Utilities", "Service Log");
- }
- VSUEventLog.Source = "Vault Server Utilities";
- VSUEventLog.Log = "Service Log";
- }
- protected override void OnStart(string[] args)
- {
- ServiceVersion = typeof(Program).Assembly.GetName().Version.ToString();
- VSUEventLog.WriteEntry(string.Format("Vault Server Utilities v{0} has started successfully.", ServiceVersion));
- }
- protected override void OnStop()
- {
- VSUEventLog.WriteEntry(string.Format("Vault Server Utilities v{0} has be shutdown.", ServiceVersion));
- }
- }
Add Comment
Please, Sign In to add comment