Example #1
0
        /// <summary>
        /// Gets the current status of the specified service.
        /// </summary>
        public bool GetServiceStatus(ServiceApp serviceApp, out ServiceStatus serviceStatus)
        {
            try
            {
                string statusFileName = PathBuilder.GetAbsolutePath(
                    new RelativePath(serviceApp, AppFolder.Log, GetStatusFileName(serviceApp)));

                if (File.Exists(statusFileName))
                {
                    using (FileStream stream =
                               new FileStream(statusFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            const int MaxLineCount = 10;
                            int       lineCount    = 0;

                            while (!reader.EndOfStream && lineCount < MaxLineCount)
                            {
                                string line = reader.ReadLine();
                                lineCount++;

                                if (line.StartsWith("Status", StringComparison.Ordinal) ||
                                    line.StartsWith("Статус", StringComparison.Ordinal))
                                {
                                    int colonIdx = line.IndexOf(':');

                                    if (colonIdx >= 0)
                                    {
                                        string s = line.Substring(colonIdx + 1).Trim();
                                        serviceStatus = ScadaUtils.ParseServiceStatus(s);
                                        return(true);
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteError(ex, Locale.IsRussian ?
                               "Ошибка при получении статуса службы" :
                               "Error getting service status");
            }

            serviceStatus = ServiceStatus.Undefined;
            return(false);
        }