public void Get(Form2Entity entity) { try { ServiceName = ConfigurationManager.AppSettings["ServiceName"]; ServiceController Sc = new ServiceController(ServiceName); sc = Sc; if (entity != null) { AccessAppSettings(entity); } } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
private void 择进程ToolStripMenuItem_Click(object sender, EventArgs e) { List <Form2Entity> list = new List <Form2Entity>(); ServiceController[] services = System.ServiceProcess.ServiceController.GetServices(); for (int i = 0; i < services.Length; i++) { Form2Entity entity = new Form2Entity(); string xxx = string.Format("{0} {1} {2}", services[i].ServiceName, services[i].DisplayName, services[i].Status.ToString()); entity.PId = services[i].ServiceName; entity.Name = services[i].DisplayName; if (services[i].Status == ServiceControllerStatus.Stopped) { entity.Describe = "停止"; } if (services[i].Status == ServiceControllerStatus.Running) { entity.Describe = "正在运行"; } list.Add(entity); } // Form2 form2 = new Form2(list); Form2 form2 = Form2.CreateInstrance(list); form2.StartPosition = FormStartPosition.CenterParent; form2.ShowDialog(this); #region MyRegion //遍历电脑中的进程 //Process[] processes = Process.GetProcesses(); //for (int i = 0; i < processes.GetLength(0); i++) //{ // Form2Entity entity = new Form2Entity(); // //我是要找到我需要的YZT.exe的进程,可以根据ProcessName属性判断 // entity.PId = processes[i].Id; // entity.Name = processes[i].ProcessName; // entity.Describe = processes[i].MainWindowTitle; // list.Add(entity); //} //遍历电脑中的服务 #endregion }
private void button3_Click(object sender, EventArgs e) { Form2Entity entity = new Form2Entity(); entity = Models.Find(h => h.PId.ToString() == textBox1.Text); if (entity == null) { MessageBox.Show("搜索的服务不存在"); return; } foreach (DataGridViewRow dgvr in dataGridView1.Rows) { SelectEntity = (Form2Entity)dgvr.DataBoundItem; if (entity.PId == SelectEntity.PId) { dataGridView1.ClearSelection(); dgvr.Selected = true; dataGridView1.CurrentCell = dgvr.Cells[1]; return; } } }
private void AccessAppSettings(Form2Entity entity) { //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings.AllKeys.Contains("ServiceName")) { //根据Key读取<add>元素的Value //string name = config.AppSettings.Settings["ServiceName"].Value; //写入<add>元素的Value config.AppSettings.Settings["ServiceName"].Value = entity.Name; } else { //增加<add>元素 config.AppSettings.Settings.Add("ServiceName", entity.Name); } //删除<add>元素 //config.AppSettings.Settings.Remove("name"); //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); //刷新,否则程序读取的还是之前的值(可能已装入内存) System.Configuration.ConfigurationManager.RefreshSection("appSettings"); }