Esempio n. 1
0
        static void UpdateServiceDescriptionWithVersion(string serviceName, string machineName, string path)
        {
            const string sv       = "Service Version: ";
            string       filepath =
                Alphaleonis.Win32.Filesystem.Path.GetFullPath("\\\\" + machineName + "\\" + path.Replace(':', '$'));
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(filepath);

            ServiceUtilWin32Helper svcUtil = new ServiceUtilWin32Helper(machineName);
            string desc = svcUtil.GetServiceDescription(serviceName);

            if (!string.IsNullOrWhiteSpace(desc))
            {
                int pos = desc.IndexOf(sv);
                if (pos >= 0)
                {
                    desc = desc.Remove(pos, desc.Length - pos);
                }
                if (desc.Length > 0 && desc[desc.Length - 1] != ' ')
                {
                    desc += " ";
                }
            }

            desc = string.Format("{0}{1}{2}", desc, sv, fvi.FileVersion);
            svcUtil.SetServiceDescription(serviceName, desc);
        }
Esempio n. 2
0
 public static void UpdateServiceDescription(string serviceName, string machineName, string description)
 {
     if (!String.IsNullOrWhiteSpace(description))
     {
         ServiceUtilWin32Helper svcUtil = new ServiceUtilWin32Helper(machineName);
         string desc = svcUtil.GetServiceDescription(serviceName);
         svcUtil.SetServiceDescription(serviceName, description);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Queries the status and other configuration properties of a service.
        /// </summary>
        /// <param name="serviceName">The internal service name. Ex:"Print Spooler" is "Spooler".</param>
        /// <param name="machineName">The server on which the service is running.</param>
        /// <returns>The service configuration details.</returns>
        public static ServiceConfig QueryService(string serviceName, string machineName)
        {
            ServiceConfig config = new ServiceConfig()
            {
                ServiceName = serviceName,
                ServerName  = machineName
            };

            ManagementObjectCollection services = GetServicesByServiceName(serviceName, machineName);

            foreach (ManagementObject service in services)
            {
                config.State        = service.GetPropertyValue("State").ToString();
                config.AcceptStop   = bool.Parse(service.GetPropertyValue("AcceptStop").ToString());
                config.DisplayName  = service.GetPropertyValue("DisplayName").ToString();
                config.LogOnAs      = service.GetPropertyValue("StartName").ToString();
                config.PathName     = service.GetPropertyValue("PathName").ToString();
                config.ProcessId    = int.Parse(service.GetPropertyValue("ProcessId").ToString());
                config.ServiceType  = service.GetPropertyValue("ServiceType").ToString();
                config.ErrorControl = service.GetPropertyValue("ErrorControl").ToString();

                config.AcceptPause       = bool.Parse(service.GetPropertyValue("AcceptPause").ToString());
                config.Caption           = service.GetPropertyValue("Caption").ToString();
                config.CheckPoint        = int.Parse(service.GetPropertyValue("CheckPoint").ToString());
                config.CreationClassName = service.GetPropertyValue("CreationClassName").ToString();
                config.DelayedAutoStart  = bool.Parse(service.GetPropertyValue("DelayedAutoStart").ToString());
                config.DesktopInteract   = bool.Parse(service.GetPropertyValue("DesktopInteract").ToString());
                config.ExitCode          = int.Parse(service.GetPropertyValue("ExitCode").ToString());
                config.InstallDate       = service.GetPropertyValue("InstallDate")?.ToString();
                config.Name = service.GetPropertyValue("Name")?.ToString();
                config.ServiceSpecificExitCode = int.Parse(service.GetPropertyValue("ServiceSpecificExitCode")?.ToString());
                config.Started   = bool.Parse(service.GetPropertyValue("Started")?.ToString());
                config.StartName = service.GetPropertyValue("StartName")?.ToString();
                config.State     = service.GetPropertyValue("State")?.ToString();
                config.Status    = service.GetPropertyValue("Status")?.ToString();
                config.SystemCreationClassName = service.GetPropertyValue("SystemCreationClassName")?.ToString();
                config.SystemName = service.GetPropertyValue("SystemName")?.ToString();
                config.TagId      = int.Parse(service.GetPropertyValue("TagId")?.ToString());
                config.WaitHint   = int.Parse(service.GetPropertyValue("WaitHint")?.ToString());

                string startMode = service.GetPropertyValue("StartMode").ToString();
                if (startMode == "Auto")
                {
                    startMode = "Automatic";
                }
                config.StartMode = (ServiceStartMode)Enum.Parse(typeof(ServiceStartMode), startMode);
            }

            try
            {
                ServiceUtilWin32Helper svcUtil = new ServiceUtilWin32Helper(machineName);
                config.Description = svcUtil.GetServiceDescription(serviceName);
            }
            catch { }             //eat the error

            return(config);
        }