Ejemplo n.º 1
0
        private List <ServiceDetailes> EnumWinServices()
        {
            try
            {
                if ((string.IsNullOrWhiteSpace(textBox_UserName.Text) == true) ||
                    (string.IsNullOrWhiteSpace(textBox_ComputerName.Text) == true) ||
                    (string.IsNullOrWhiteSpace(textBox_Pwd.Text) == true))
                {
                    throw new Exception("The machine name, user name or passowd is empty.");
                }

                List <ServiceDetailes> list = new List <ServiceDetailes>();

                ServiceController[] services = null;

                m_Settings.Domain      = textBox_Domain.Text;
                m_Settings.UserName    = textBox_UserName.Text;
                m_Settings.MachineName = textBox_ComputerName.Text;
                m_Settings.Filter      = textBox_Filter.Text;

                using (new Impersonation(m_Settings.Domain
                                         , m_Settings.UserName
                                         , textBox_Pwd.Text))
                {
                    services = ServiceController.GetServices(m_Settings.MachineName);
                }

                if (services != null)
                {
                    foreach (ServiceController service in services)
                    {
                        if (string.IsNullOrWhiteSpace(m_Settings.Filter) == false)
                        {
                            if (service.DisplayName.StartsWith(m_Settings.Filter, true, CultureInfo.CurrentUICulture) == true)
                            {
                                list.Add(new ServiceDetailes(service.DisplayName, service.Status));
                            }
                        }
                        else
                        {
                            list.Add(new ServiceDetailes(service.DisplayName, service.Status));
                        }
                    }

                    if (SettingsMngr.SaveSettings(m_Settings) == false)
                    {
                        throw new Exception("Failed to save settings.");
                    }
                }

                return(list);
            }
            catch (Exception exp)
            {
                Logger.WriteError(exp, "34e3822c-1c49-4076-98e0-4d89ad554a7f");

                throw exp;
            }
        }
Ejemplo n.º 2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                m_Settings = SettingsMngr.GetSettings();

                if (m_Settings == null)
                {
                    m_Settings             = new Settings();
                    m_Settings.MachineName = @"";
                    m_Settings.Domain      = @"";
                    m_Settings.UserName    = @"";
                    m_Settings.Filter      = @"";

                    SettingsMngr.SaveSettings(m_Settings);

                    m_Settings = SettingsMngr.GetSettings();
                }

                textBox_Domain.Text       = m_Settings.Domain;
                textBox_UserName.Text     = m_Settings.UserName;
                textBox_ComputerName.Text = m_Settings.MachineName;
                textBox_Filter.Text       = m_Settings.Filter;

                button_Go.Focus();
            }
            catch (Exception exp)
            {
                Logger.WriteError(exp, "c0e6736c-0c81-45f1-b971-a9c4345aa5ac");

                MessageBox.Show(exp.Message, "WinServicesStatus", MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }