使用WMI控制Windows进程 和服务
生活随笔
收集整理的這篇文章主要介紹了
使用WMI控制Windows进程 和服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.使用WMI控制Windows進程
本文主要介紹兩種WMI的進行操作:檢查進程是否存在、創建新進行
代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Threading;
namespace TJVictor.WMI
{
public class Win32_Process:WMIBaseClass
{
#region Property
private int timeout = 30;
public int TimeOut
{
get { return timeout; }
set { timeout = value; }
}
private string wqlSelect = "select * FROM Win32_Process where Name='{0}'";
#endregion
#region Construction
public Win32_Process()
: base()
{
base.Connection();
}
public Win32_Process(string domain, string Ip, string user, string psd)
: base(domain, Ip, user, psd)
{
base.Connection();
}
#endregion
#region public function
public bool IsProcessExist(string name)
{
if (!GetSelectQueryCollection(wqlSelect,name).Count.Equals(0))
return true;
return false;
}
public void CreateProcess(string name)
{
ManagementClass processClass = new ManagementClass("Win32_Process");
processClass.Scope = base.Scope;
ManagementBaseObject mbo = processClass.GetMethodParameters("Create");
mbo["CommandLine"] = string.Format(name);
ManagementBaseObject result = processClass.InvokeMethod("Create", mbo, null);
//檢測執行結果
CheckExceptionClass.CheckProcessException(int.Parse(result["returnValue"].ToString()));
//檢測進程是否執行完成
int tempTimeout = this.timeout;
while (!GetSelectQueryCollection("select * FROM Win32_Process where ProcessID='{0}'",result["ProcessID"].ToString()).Count.Equals(0))
{
if (tempTimeout.Equals(0))
{
throw new TJVictor.WMI.WmiException.ProcessException(
string.Format("在 {0} 上執行 {1} 操作失敗,執行超時", base.Ip, name));
}
tempTimeout--;
Thread.Sleep(1000);
}
}
#endregion
}
}
2.使用WMI來控制Windows服務
本文介紹如何使用WMI來判斷服務是否存在、如何創建新服務,刪除服務、如何啟服務、停服務
代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Threading;
namespace TJVictor.WMI
{
public class Win32_Service:WMIBaseClass
{
#region Property
private bool completed = false;
private int timeout = 30;
public int TimeOut
{
get { return timeout; }
set { timeout = value; }
}
private string wqlSelect = string.Empty;
#endregion
#region Construction
public Win32_Service()
: base()
{
wqlSelect = "select * FROM Win32_Service where Name='{0}'";
base.Connection();
}
public Win32_Service(string domain, string Ip, string user, string psd)
: base(domain, Ip, user, psd)
{
wqlSelect = "select * FROM Win32_Service where Name='{0}'";
base.Connection();
}
#endregion
#region public function
public bool IsServiceExist(string name)
{
if (!GetSelectQueryCollection(wqlSelect,name).Count.Equals(0))
return true;
return false;
}
public void StartService(string name)
{
if(!IsServiceExist(name))
throw new TJVictor.WMI.WmiException.ServiceException(string.Format("{0} 服務不存在",name));
object result = string.Empty;
ManagementObjectSearcher mos = GetObjectSearcher(wqlSelect, name);
foreach (ManagementObject mo in mos.Get())
{
result = mo.InvokeMethod("StartService", null);
break;
}
CheckExceptionClass.CheckServiceException(int.Parse(result.ToString()));
TestServiceState(mos, "Running", string.Format("{0} 服務在 {1} 機器上啟動失敗,啟動超時",name,base.Ip));
}
public void StopService(string name)
{
if (!IsServiceExist(name))
throw new TJVictor.WMI.WmiException.ServiceException(string.Format("{0} 服務不存在", name));
object result = string.Empty;
ManagementObjectSearcher mos = GetObjectSearcher(wqlSelect, name);
foreach (ManagementObject mo in mos.Get())
{
result = mo.InvokeMethod("StopService", null);
break;
}
CheckExceptionClass.CheckServiceException(int.Parse(result.ToString()));
TestServiceState(mos, "Stopped", string.Format("{0} 服務在 {1} 機器上停止失敗,停止超時", name, base.Ip));
}
public void CreateService(string name, string displayName, string startMode, string pathName, string startName, string startPassword,
string serviceType)
{
if(IsServiceExist(name))
throw new TJVictor.WMI.WmiException.ServiceException(string.Format("{0} 服務已經存在",name));
int tempTimeout = this.timeout;
ManagementClass processClass = new ManagementClass("Win32_Service");
processClass.Scope = base.Scope;
string method = "Create";
ManagementBaseObject inputArgs = processClass.GetMethodParameters(method);
inputArgs["Name"] = name;
inputArgs["DisplayName"] = displayName;
inputArgs["StartMode"] = startMode;
inputArgs["PathName"] = pathName;
if (!startName.Equals(string.Empty))
{
inputArgs["StartName"] = startName;
inputArgs["StartPassword"] = startPassword;
}
ManagementBaseObject ob = processClass.InvokeMethod(method, inputArgs, null);
CheckExceptionClass.CheckServiceException(int.Parse(ob["returnValue"].ToString()));
//檢測服務是否已經安裝成功
while (!IsServiceExist(name))
{
if (tempTimeout.Equals(0))
{
throw new TJVictor.WMI.WmiException.ServiceException(
string.Format("在 {0} 上安裝 {1} 服務超時", base.Ip, name));
}
Thread.Sleep(1000);
tempTimeout--;
}
}
public void DeleteService(string name)
{
object result = string.Empty;
if(!IsServiceExist(name))
throw new TJVictor.WMI.WmiException.ServiceException(string.Format("{0} 服務不存在",name));
foreach (ManagementObject mo in GetSelectQueryCollection(wqlSelect, name))
{
result = mo.InvokeMethod("delete", null);
break;
}
//檢測卸載命令是否執行成功
CheckExceptionClass.CheckServiceException(int.Parse(result.ToString()));
//檢測服務是否已經卸載成功
int tempTimeout = this.timeout;
while (IsServiceExist(name))
{
if (tempTimeout.Equals(0))
{
throw new TJVictor.WMI.WmiException.ServiceException(
string.Format("在 {0} 上卸載 {1} 服務超時", base.Ip, name));
}
Thread.Sleep(1000);
tempTimeout--;
}
}
#endregion
#region private function
private void TestServiceState(ManagementObjectSearcher mos, string state,string errorMes)
{
completed = false;
int tempTimeout = timeout;
while (!completed)
{
if (tempTimeout.Equals(0))
{
throw new TJVictor.WMI.WmiException.ServiceException(errorMes);
}
foreach (ManagementObject mo in mos.Get())
{
if (mo["State"].ToString().Equals(state))
{
completed = true;
}
break;
}
Thread.Sleep(1000);
tempTimeout--;
}
}
#endregion
}
}
總結
以上是生活随笔為你收集整理的使用WMI控制Windows进程 和服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 香港一小巴侧翻致1死16伤 警方:未见机
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?