c#操作进程、服务、注册表(源码测试通过)

来源:互联网 发布:修改积分软件. 编辑:程序博客网 时间:2024/05/21 17:31

1、操作进程:

 private bool CloseProcess(string CloseProcessName)        {            try            {                //根据进程名称,获取该进程信息                Process[] MyProcessS = Process.GetProcessesByName(CloseProcessName);                        foreach (Process MyProcess in MyProcessS)                {                    MyProcess.Kill();                    MyProcess.WaitForExit();                    MyProcess.Close();                    Thread.Sleep(10000);                  }            }            catch (Exception)            {                return false;                                             }            return true;                                  }        /// <summary>        /// 创建进程        /// </summary>        public bool StartProcess(string StartProPath)        {            try            {                Process TheStartProcess = Process.Start(StartProPath);                        }            catch (Exception)            {                return false;                                                }            return true;                                                }


2、操作服务:

  private bool StopService(string StopServiceName)        {               ServiceController service = new ServiceController(StopServiceName);             try              {                    service.Stop();                    service.WaitForStatus(ServiceControllerStatus.Stopped);             }               catch(Exception)              {                return false;            }            return true;        }        /// <summary>        /// 开启服务        /// </summary>        private bool StartService(string StartServiceName)         {              ServiceController service = new ServiceController(StartServiceName);             try              {                     service.Start();                    service.WaitForStatus(ServiceControllerStatus.Running);            }               catch (Exception)            {                return false;            }            return true;        }

2.1补充操作服务代码改进:

 /// <summary>        /// 停止服务        /// </summary>        private bool StopService(string StopServiceName)        {               ServiceController service = new ServiceController(StopServiceName);             try              {                if (service.Status == ServiceControllerStatus.Running)                {                    service.Stop();                    service.WaitForStatus(ServiceControllerStatus.Stopped);                    if (service.Status == ServiceControllerStatus.Stopped)                    {                        return true;                    }                    else                    {                        //记录错误log                        return false;                    }                }                else                {                    return true;                }            }               catch(Exception)              {                return false;            }        }        /// <summary>        /// 开启服务        /// </summary>        private bool StartService(string StartServiceName)         {              ServiceController service = new ServiceController(StartServiceName);             try              {                if (service.Status == ServiceControllerStatus.Stopped)                {                    service.Start();                    service.WaitForStatus(ServiceControllerStatus.Running);                    if (service.Status == ServiceControllerStatus.Running)                    {                        return true;                    }                    else                    {                        //记录log                        return false;                    }                }                else                {                    service.Refresh();                    return true;                }            }               catch (Exception)            {                return false;            }        }

3、操作注册表:

  
    ///获得注册表的值
    private string  GetRegistShellData(string RegistName)        {            try            {                string registData, SubregistData;                RegistryKey hkml = Registry.LocalMachine;                RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);                registData = software.GetValue(RegistName).ToString();                SubregistData = registData.Substring(0, 2);                return SubregistData;            }            catch (Exception excp)            {                MessageBox.Show("GetRegistShellData错误" + excp.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);            }            return "";        }        /// <summary>
        /// </summary>        ///更改注册表的值
        private void  RenameRegistData()        {            try            {                string registData1;                RegistryKey hkml = Registry.LocalMachine;                RegistryKey software2 = hkml.OpenSubKey(@"SOFTWARE\"+ SaiWeb + @"\SysToolSign", true);                registData1 = software2.GetValue("Sign").ToString();                software2.SetValue("Sign", "1");                registData1 = software2.GetValue("Sign").ToString();            }            catch (Exception excp)            {                MessageBox.Show("RenameRegistData错误" + excp.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);            }            return ;        }


 

原创粉丝点击