示例#1
0
        /// <summary>
        /// Changes the state of the specified Windows service using the
        /// specified action.
        /// </summary>
        /// <param name="host">The name of the host to connect to where the Windows service resides.</param>
        /// <param name="serviceName">The name of the Windows service.</param>
        /// <param name="action">The action in which to perform on the Windows service.</param>
        /// <returns>A value indicating if the state of the Windows service was changed.</returns>
        public bool ChangeServiceState(string host, string serviceName, WindowsServiceAction action)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(serviceName))
                {
                    return(false);
                }

                var processInfo = CreateProcessInfo(string.Format("\\\\{0} {1} \"{2}\"", host, action.ToString().ToLower(), serviceName));
                if (processInfo == null)
                {
                    return(false);
                }

                string output;
                string errors;

                ProcessUtils.ExecuteProcess(processInfo, out output, out errors);

                return(!output.Contains(FailedState));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Changes the state of the specified Windows service using the
        /// specified action.
        /// </summary>
        /// <param name="host">The name of the host to connect to where the Windows service resides.</param>
        /// <param name="serviceName">The name of the Windows service.</param>
        /// <param name="action">The action in which to perform on the Windows service.</param>
        /// <returns>A value indicating if the state of the Windows service was changed.</returns>
        public bool ChangeServiceState(string host,string serviceName,WindowsServiceAction action)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(serviceName)) 
                    return false;

                var processInfo = CreateProcessInfo(string.Format("\\\\{0} {1} \"{2}\"", host, action.ToString().ToLower(), serviceName));
                if (processInfo == null)
                    return false;

                string output;
                string errors;

                ProcessUtils.ExecuteProcess(processInfo, out output, out errors);

                return !output.Contains(FailedState);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return false;
            }
        }