Home » .Net Framework

How to start and stop windows Services on remote machine using C#

Initially I created win application with ServiceController to restart remote windows service, I success to restart remote windows service.

then I implement same code in windows service to restart remote windows service, but it gives some error message like "can not open 'service name' on computer 'machine name'.

In serviceProcessInstaller properties i given like Account = LocalSystem

Note i also check the Service Manager on my local computer and connect it to the remote computer, there i able to restart the service.

then why its not allowing to restart the service from windows service

Please suggest me....




2 Answers Found


Answer 1

check this...



using System;
using System.Management; 
namespace WMITest
class clsMain
static void Main(string[] args)

static void GetWMIStats()
//Connection credentials to the remote  computer -
//logged in account  must have adminstrator access for a remote machine
ConnectionOptions oConn = new ConnectionOptions();
//oConn.Username = "username";
//oConn.Password = "password";
string remoteMachineName="localhost";
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" +remoteMachineName , oConn);     
// pass the name of the service  to query with this particular WQL query---
System.Management.RelatedObjectQuery oQuery =
new System.Management.RelatedObjectQuery("Win32_Service.Name='Alerter'");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();  

foreach( ManagementObject oReturn in oReturnCollection )
Console.WriteLine("Name : " + oReturn["Name"].ToString());

// Repeat the above process for each property of the respective
// object that can be read. See the WMI documentation at MSDN for details.




Answer 2

Hi praveen thank you for reply

i am using ServiceController class i think i am missing something on settings like  

In serviceProcessInstaller properties  i given like Account = LocalSystem.




<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure