网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

WMI使用教程(译)

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/12
orms.MouseEventArgs e)
{
System.Windows.Forms.ListView listViewObject = (System.Windows.Forms.ListView) sender;
ContextMenu mnuContextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem();
ManagementObjectCollection queryCollection;

//check if right button
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//get service name
ServiceName = listViewObject.GetItemAt(e.X, e.Y).Text;
//set list view item
ServiceItem = listViewObject.GetItemAt(e.X,e.Y);

//create popup menu
listViewObject.ContextMenu = mnuContextMenu;
try
{
//get specific service object
queryCollection = getServiceCollection("SELECT * FROM Win32_Service Where Name = ''" +
ServiceName + "''");
foreach ( ManagementObject mo in queryCollection)
{
//create menu depending on service state
if (mo["Started"].Equals(true))
{
menuItem.Text = "Stop";
//set action property
ServiceAction = "StopService";
}
else
{
menuItem.Text = "Start";
ServiceAction = "StartService";
}
mnuContextMenu.MenuItems.Add(menuItem);

// Add functionality to the menu items using the Click event.
menuItem.Click += new System.EventHandler(this.menuItem_Click);
}
}
catch (Exception e1)
{
MessageBox.Show("Error: " + e1);
}
}
}

/// <summary>
/// List view context menu click event to invoke start/stop service
/// </summary>
///
///
private void menuItem_Click(object sender, System.EventArgs e)
{
ManagementObjectCollection queryCollection;
ListViewItem lvItem;

//Set up a handler for the asynchronous callback
ManagementOperationObserver observer = new ManagementOperationObserver();
completionHandler.MyHandler completionHandlerObj = new completionHandler.MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);

//get specific service object
queryCollection = getServiceCollection("Select * from Win32_Service Where Name =''" +
ServiceName + "''");

//Status
updateStatus("Starting/Stopping service");

foreach ( ManagementObject mo in queryCollection)
{
//start or stop service
mo.InvokeMethod(observer, ServiceAction, null);
}
//wait until invoke method is complete or 5 sec timeout

int intCount = 0;
while
(!completionHandlerObj.IsComplete)
{
if
(intCount > 10)
{
MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
break;
}
//wait 1/2 sec.
System.Threading.Thread.Sleep(500);

//increment counter
intCount++;
}

//see if call was successful.
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
//succeeded
lvItem = ServiceItem;

if (ServiceAction == "StartService")
lvItem.SubItems.Text = "Started";
else
lvItem.SubItems.Text = "Stop";
}
else
{
//error message
string stringAction;

if (ServiceAction == "StartService"

网学推荐

免费论文

原创论文

设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号