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 = ;
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())
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())
{
if (tempTimeout.Equals())
{
throw new TJVictor.WMI.WmiException.ProcessException(
string.Format("在 {0} 上执行 {1} 操作失败,执行超时", base.Ip, name));
}
tempTimeout--;
Thread.Sleep();
}
}
#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 = ;
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())
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())
{
throw new TJVictor.WMI.WmiException.ServiceException(
string.Format("在 {0} 上安装 {1} 服务超时", base.Ip, name));
}
Thread.Sleep();
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())
{
throw new TJVictor.WMI.WmiException.ServiceException(
string.Format("在 {0} 上卸载 {1} 服务超时", base.Ip, name));
}
Thread.Sleep();
tempTimeout--;
}
}
#endregion #region private function
private void TestServiceState(ManagementObjectSearcher mos, string state,string errorMes)
{
completed = false;
int tempTimeout = timeout;
while (!completed)
{
if (tempTimeout.Equals())
{
throw new TJVictor.WMI.WmiException.ServiceException(errorMes);
}
foreach (ManagementObject mo in mos.Get())
{
if (mo["State"].ToString().Equals(state))
{
completed = true;
}
break;
}
Thread.Sleep();
tempTimeout--;
}
}
#endregion
}
}

最新文章

  1. C# ini文件操作【源码下载】
  2. cookie的session_id解释
  3. Spark数据传输及ShuffleClient(源码阅读五)
  4. javascript 正则表达式使用
  5. 领导者/追随者(Leader/Followers)模型和半同步/半异步(half-sync/half-async)模型都是常用的客户-服务器编程模型
  6. .net,微软,薪资及其他
  7. Java-Preferences用法-入门
  8. abp允许跨域代码,时间转换为固定格式,本地时间
  9. CSS3标准盒模型还是IE怪异模型box-sizing属性的应用设置
  10. AND 初识
  11. 333. Largest BST Subtree节点数最多的bst子树
  12. 利用快排partition求前N小的元素
  13. Mac 下 java环境 maven环境配置
  14. SGU---107 水题
  15. mysql--数据库的简单认识
  16. 《DSP using MATLAB》示例Example 8.21
  17. 应用性能管理(APM, Application Performance Management)
  18. JavaScript中创建自定义对象的方法
  19. linux 查看页大小
  20. npm安装使用淘宝代理的方法(设置registry参数)

热门文章

  1. 物联网传输协议MQTT
  2. Linux kernel ‘net/key/af_key.c’信息泄露漏洞
  3. HDU 2614 Beat 深搜DFS
  4. 字符串[未AC](后缀自动机):HEOI 2016 str
  5. 数据结构(跳跃表):NOI 2004 郁闷的出纳员
  6. 汇编学习笔记(3)[bx]和loop
  7. HDOJ 2076 夹角有多大(题目已修改,注意读题)
  8. log4j配置祥解
  9. .NET 基础 一步步 一幕幕[面向对象之new、this关键字]
  10. JVM运行数据区