コード例 #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);
        }
コード例 #2
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 QueryStatus(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());

                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);
        }