private void setButtonLabel() { try { String[] strArr; ServiceController scSendMail = null; BusinessServices.AppConfig ac = new BusinessServices.AppConfig(); DataTable dt = ac.getMailServices(); foreach (DataRow dr in dt.Rows) { strArr = dr.ItemArray[1].ToString().Split(';'); if (dr.ItemArray[0].Equals("MailService_SendMail")) { try { scSendMail = new ServiceController(strArr[1], strArr[0]); } catch (Exception e) { btnServiceToggle.Text = "- No Service Permissions -"; } } } if (scSendMail.Status != ServiceControllerStatus.Stopped) { btnServiceToggle.Text = ResourceManager.GetString("btnStopServices"); } else { btnServiceToggle.Text = ResourceManager.GetString("btnStartServices"); } } catch (Exception e) { ErrorHandler.ErrorLog el = new ErrorHandler.ErrorLog(e, ErrorLevel.High, "Service Control", "No Permissions to access", "Please use subinacl.exe to grant permissions"); } }
protected void btnServiceToggle_Click(object sender, EventArgs e) { try { BusinessServices.AppConfig ac = new BusinessServices.AppConfig(); BusinessServices.Email em = new BusinessServices.Email(); DataTable dt = ac.getMailServices(); String[] strArr; ServiceController scQueueMail = null; ServiceController scQueueReports = null; ServiceController scSendMail = null; foreach (DataRow dr in dt.Rows) { strArr = dr.ItemArray[1].ToString().Split(';'); if (dr.ItemArray[0].Equals("MailService_QueueMail")) { scQueueMail = new ServiceController(strArr[1], strArr[0]); } else if (dr.ItemArray[0].Equals("MailService_QueueReports")) { scQueueReports = new ServiceController(strArr[1], strArr[0]); } else if (dr.ItemArray[0].Equals("MailService_SendMail")) { scSendMail = new ServiceController(strArr[1], strArr[0]); } } //String username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;// used for debugging // if its stopped then start it if (scSendMail.Status == ServiceControllerStatus.Stopped) { em.purgeAutoEmails(); // set the send mail flag to allow emails to be sent ac.Update("SEND_AUTO_EMAILS", "YES"); scSendMail.Start(); scSendMail.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10)); scSendMail.Refresh(); } // if its started then stop it else if (scSendMail.Status == ServiceControllerStatus.Running) { em.purgeAutoEmails(); // set the send mail flag to stop emails from being sent ac.Update("SEND_AUTO_EMAILS", "NO"); // restart report queuing restartService(scQueueReports); scSendMail.Stop(); scSendMail.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10)); scSendMail.Refresh(); // restart mail queuing service restartService(scQueueMail); } //username = System.Security.Principal.WindowsIdentity.GetCurrent().Name; setButtonLabel(); } catch (Exception ex) { ErrorHandler.ErrorLog el = new ErrorHandler.ErrorLog(ex, ErrorLevel.High, "Service Control", "Panic button pressed", ex.Message); } }