예제 #1
0
 private void btnServerSet_Click(object sender, EventArgs e)
 {
     try
     {
         FormMain dlg = new FormMain(true);
         dlg.ShowDialog();
         if (MessageBox.Show("服务配置修改后需重启服务,是否重启服务?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
         {
             if (ServiceAPI.StopService(strServiceName))
             {
                 Thread.Sleep(1000);
                 ServiceAPI.RunService(strServiceName);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
예제 #2
0
        //启动服务
        void OnService(string serviceName, Button btn, Label txt)
        {
            try
            {
                if (btn.Text == "启动服务")//启动服务
                {
                    ServiceAPI.RunService(serviceName);

                    int status = ServiceAPI.GetServiceStatus(serviceName);
                    if (status == 2 || status == 4 || status == 5)
                    {
                        txt.Text = "服务【" + serviceName + "】启动成功!";
                        btn.Text = "停止服务";
                    }
                    else
                    {
                        txt.Text = "服务【" + serviceName + "】启动失败!";
                    }
                }
                else //关闭服务
                {
                    ServiceAPI.StopService(serviceName);

                    int status = ServiceAPI.GetServiceStatus(serviceName);
                    if (status == 1 || status == 3 || status == 6 || status == 7)
                    {
                        txt.Text = "服务【" + serviceName + "】停止成功!";
                        btn.Text = "启动服务";
                    }
                    else
                    {
                        txt.Text = "服务【" + serviceName + "】停止失败!";
                    }
                }
            }
            catch (Exception ex)
            {
                txt.Text = "error";
                LogAPI.WriteLog(ex.Message);
            }
        }