游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1104|回复: 1

??C#??????????

[复制链接]

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2004-8-5 13:31:00 | 显示全部楼层 |阅读模式
?.net?????????????Windows???????????????????????????System.ServiceProcess???????ServiceController ??System.Management?????WMI????????ServiceController?????????????????????????????????????????WMI??????????????????????????????????????????????????????????????????System.Management??????????????????
        WMI??Windows 2000???????????????,????????.???????CIM???????????????DMTF?????????????????????????????????????????Windows???????CIM for Windows??????????????????????????WMI???????MSDN?System.Management????????????????????????????????? Windows ???? (WMI) ??????????????????????
        ???????????????????????????
??????????System.Management.Dll????????System.Management?????????ManagementClass,ManagementObject?????????Win32ServiceManager???????????????????????
using System;
using System.Management;
namespace ZZ.Wmi
{
     public class Win32ServiceManager
     {
         private string strPath;
         private ManagementClass managementClass;
         public Win32ServiceManager():this(".",null,null)
         {
         }
         public Win32ServiceManager(string host,string userName,string password)
         {
              this.strPath = "\\\\"+host+"\\root\\cimv2:Win32_Service";
              this.managementClass = new ManagementClass(strPath);
               if(userName!=null&&userName.Length>0)
              {
                   ConnectionOptions connectionOptions = new ConnectionOptions();
                   connectionOptions.Username = userName;
                   connectionOptions.Password = password;
                   ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
                   this.managementClass.Scope = managementScope;
              }
         }
         // ?????????????
         public static bool RemoteConnectValidate(string host,string userName,string password)
         {
              ConnectionOptions connectionOptions = new ConnectionOptions();
              connectionOptions.Username = userName;
              connectionOptions.Password = password;
              ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
              try
              {
                   managementScope.Connect();
              }
              catch
              {
              }
              return managementScope.IsConnected;
         }
         // ??????????
         public object GetServiceValue(string serviceName,string propertyName)
         {
              ManagementObject mo = this.managementClass.CreateInstance();
              mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
              return mo[propertyName];
         }
         // ????????????????
         public string [,] GetServiceList()
         {
              string [,] services = new string [this.managementClass.GetInstances().Count,4];
              int i = 0;
              foreach(ManagementObject mo in this.managementClass.GetInstances())
              {
                   services[i,0] = (string)mo["Name"];
                   services[i,1] = (string)mo["DisplayName"];
                   services[i,2] = (string)mo["State"];
                   services[i,3] = (string)mo["StartMode"];
                   i++;
              }
              return services;
         }
         // ????????????????
         public string [,] GetServiceList(string serverName)
         {
              return GetServiceList(new string []{serverName});
         }
         // ?????????????????
         public string [,] GetServiceList(string [] serverNames)
         {
              string [,] services = new string [serverNames.Length,4];
              ManagementObject mo = this.managementClass.CreateInstance();
              for(int i = 0;i<serverNames.Length;i++)
              {
                   mo.Path = new ManagementPath(this.strPath+".Name=\""+serverNames+"\"");
                   services[i,0] = (string)mo["Name"];
                   services[i,1] = (string)mo["DisplayName"];
                   services[i,2] = (string)mo["State"];
                   services[i,3] = (string)mo["StartMode"];
              }
              return services;
         }
         // ???????
         public string StartService(string serviceName)
         {
              string strRst = null;
              ManagementObject mo = this.managementClass.CreateInstance();
              mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
              try
              {
                   if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]
                       mo.InvokeMethod("StartService",null);
              }
              catch(ManagementException e)
              {
                   strRst =e.Message;
              }
              return strRst;
         }
         // ???????
         public string PauseService(string serviceName)
         {
              string strRst = null;
              ManagementObject mo = this.managementClass.CreateInstance();
              mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
              try
              {
                   //????????
                   if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")
                       mo.InvokeMethod(&quotauseService",null);
              }
              catch(ManagementException e)
              {
                   strRst =e.Message;
              }
              return strRst;
         }
         // ???????
         public string ResumeService(string serviceName)
         {
              string strRst = null;
              ManagementObject mo = this.managementClass.CreateInstance();
              mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
              try
              {
                   //????????
                   if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")
                       mo.InvokeMethod("ResumeService",null);
              }
              catch(ManagementException e)
              {
                   strRst =e.Message;
              }
              return strRst;
         }
         // ???????
         public string StopService(string serviceName)
         {
              string strRst = null;
              ManagementObject mo = this.managementClass.CreateInstance();
              mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
              try
              {
                   //????????
                   if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"
                       mo.InvokeMethod("StopService",null);
              }
              catch(ManagementException e)
              {
                   strRst =e.Message;
              }
              return strRst;
         }
    }
}
?Win32ServiceManager???RemoteConnectValidate???????????????????GetServiceValue???GetServiceList???????????????????????????????????
     ????????????????
????????

??vs.net 2003?????????????????????????


using ZZ.Wmi;
namespace ZZForm
{
     public class Form1 : System.Windows.Forms.Form
     {
         //
         private Win32ServiceManager serviceManager;
         public Form1()
         {
              InitializeComponent();
              this.serviceManager = null;
         }
         //
         [STAThread]
         static void Main()
         {
              Application.Run(new Form1());
         }
         //??????
         private void buttonChangeState_Click(object sender, System.EventArgs e)
         {
              switch(((Button)sender).Text)
              {
                   case "??":
                       string startRst = this.serviceManager.StartService(this.listViewService.SelectedItems[0].SubItems[0].Text);
                       if(startRst==null)
                            MessageBox.Show("????,?????????????!");
                       else
                            MessageBox.Show(startRst);
                       break;
                   case "??":
                       string startPause = this.serviceManager.PauseService(this.listViewService.SelectedItems[0].SubItems[0].Text);
                       if(startPause==null)
                            MessageBox.Show("????,?????????????!");
                       else
                            MessageBox.Show(startPause);
                       break;
                   case "??":
                       string startResume = this.serviceManager.ResumeService(this.listViewService.SelectedItems[0].SubItems[0].Text);
                       if(startResume==null)
                            MessageBox.Show("????,?????????????!");
                       else
                            MessageBox.Show(startResume);
                       break;
                   case "??":
                       string startStop = this.serviceManager.StopService(this.listViewService.SelectedItems[0].SubItems[0].Text);
                       if(startStop==null)
                            MessageBox.Show("????,?????????????!");
                       else
                            MessageBox.Show(startStop);
                       break;
              }
         }
         //???????
         private void buttonLoadRefresh_Click(object sender, System.EventArgs e)
         {
              if(this.textBoxHost.Text.Trim().Length>0)
              {
                   if(this.textBoxHost.Text.Trim()==".")
                   {
                       this.serviceManager = new Win32ServiceManager();
                   }
                   else
                   {
                        if(Win32ServiceManager.RemoteConnectValidate(this.textBoxHost.Text.Trim(),this.textBoxName.Text.Trim(),this.textBoxPassword.Text.Trim()))
                       {
                            this.serviceManager = new Win32ServiceManager(this.textBoxHost.Text.Trim(),this.textBoxName.Text.Trim(),this.textBoxPassword.Text.Trim());
                       }
                       else
                       {
                            MessageBox.Show("????????????.");
                            return;
                       }
                   }
                   string [,] services = serviceManager.GetServiceList();
                   this.listViewService.BeginUpdate();
                   this.listViewService.Items.Clear();
                   for(int i=0;i<services.GetLength(0);i++)
                   {
                       ListViewItem item = new ListViewItem(new string[]{services[i,0],services[i,1],services[i,2],services[i,3]});
                       this.listViewService.Items.Add(item);
                   }
                   this.listViewService.EndUpdate();
              }
              else
                   MessageBox.Show("????????IP??");
         }
    }
}
     ????????????????????????????????????ManagementClass??????Properties???Methods????????????????Win32ServiceManager????????????ManagementObejct?????????????????????????????
     ???????System.Management?????????????????\root\cimv2:Win32_Service??????????????????????????????????????????????????????????????????????

109

主题

1451

帖子

1475

积分

金牌会员

??

Rank: 6Rank: 6

积分
1475
发表于 2004-8-5 13:52:00 | 显示全部楼层

Re: ??C#??????????

[em16]????!???????.....
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-16 11:38

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表