How to list all services and their states on a remote computer (Using C#)?
ယခု တစ္ပတ္ အတြက္ ကြန္ပ်ဴတာ တစ္ခုအတြင္းမွာ ရိွေသာ Services မ်ားကို ေဖာ္ထုတ္ေသာ နည္းကိုေဖာ္ျပ လိုက္ရပါသည္။
// IMPORTANT: Add 'System.Management' to
// your References in your Project
using System;
using System.IO;
using System.Management;
namespace ListServices
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
ManagementObjectSearcher query;
ManagementObjectCollection queryCollection = null;
System.Management.ObjectQuery oq;
string strComputerName;
string strQuery = "SELECT * FROM Win32_Service";
Console.WriteLine( "Check out http://www.mr-programmer.com for more samples, components and software\r\n" );
// Ask for a computer name
do
{
Console.Write( "Please enter server name (type 'localhost' for local machine): " );
strComputerName = Console.ReadLine();
} while( strComputerName == "" );
// Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
if (strComputerName.Trim().Length == 0)
{
Console.WriteLine("Must enter machine IP address or name.");
return;
}
//Point to machine
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + strComputerName + "\\root\\cimv2", co);
//Query remote computer across the connection
oq = new System.Management.ObjectQuery(strQuery);
query = new ManagementObjectSearcher(ms,oq);
try
{
queryCollection = query.Get();
}
catch (Exception e1)
{
Console.WriteLine( "Error: " + e1 );
}
foreach ( ManagementObject mo in queryCollection )
{
//create child node for operating system
Console.WriteLine( mo["Name"].ToString() + " [" + mo["StartMode"].ToString() + "]" );
}
}
}
}
No comments:
Post a Comment