Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team,

I have a task where i have to build a windows application and from there i have to get the services status (Start/Stopped) where i have to login remotely to other machines into DataGridView.


I have tried and now i'm able to get the service for my computer. Now how can i get the services from other machine (Where i used to connect remotely from my laptop). On that remote machine, I do have services icon on desktop. When i open that, i do have services for many servers. So can you help me achieving this?
Below is my code for fetching the services from my system:
private void button1_Click(object sender, EventArgs e)
{

ServiceController[] se = ServiceController.GetDevices();
ServiceController[] services = ServiceController.GetServices();


int i = 0;

foreach (ServiceController service in services)
{
dataGridView1.Rows.Add();
service.ServiceName = "ALG"; //Finding the paticular service
dataGridView1.Rows[i].Cells["dgvcServiceName"].Value = Convert.ToString(service.ServiceName);
dataGridView1.Rows[i].Cells["dgvcStatus"].Value = Convert.ToString(service.Status);
dataGridView1.Rows[i].Cells["dgvcDescription"].Value = Convert.ToString(service.DisplayName);
i += 1;
}

What I have tried:

I tried taking the Windows Service form at the time of creating the project but as my requirement should be in windows form, i'm little confused how and where to start?
And i took one winform and created a button and datagridview.
I started coding under button click (As after clicking the button, i want that data to be in my gridview). Below is my code:

private void button1_Click(object sender, EventArgs e)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
try
{
if (service.ServiceName == serviceName)
return true;
}
return false;
}
catch(Exception ex)
{
MessageBox.Show("Error");
}
}

But i got the error with retun word.
Posted
Updated 23-Feb-16 21:59pm
v3
Comments
Richard Deeming 19-Feb-16 11:21am    
What do you expect - you're trying to return a value from a method that doesn't have a return value!

And your code is only checking whether the service exists on the local machine, not whether the service is currently running. There's no attempt to access a remote machine.

What are you actually trying to do?
Member 8010354 22-Feb-16 2:22am    
Actually, i would like to get the service and it's status into my datagridview on a button click.
Below is the code:
private void button1_Click(object sender, EventArgs e)
{
ServiceController[] se = ServiceController.GetDevices();
ServiceController[] services = ServiceController.GetServices();
int i = 0;
foreach (ServiceController service in services)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["dgvcServiceName"].Value = Convert.ToString(service.ServiceName);
dataGridView1.Rows[i].Cells["dgvcStatus"].Value = Convert.ToString(service.Status);
i += 1;
}
}
I'm getting error near: dataGridView1.Rows.Add(); Error:No row can be added to datagridview control that does ot have columns. columns must be added first.
Richard Deeming 22-Feb-16 7:43am    
So do what the error tells you to - add some columns to the grid before you try to add the rows.
Member 8010354 24-Feb-16 2:24am    
Hi,

I have tried and now i'm able to get the service for my computer. Now how can i get the services from other machine (Where i used to connect remotely from my laptop). On that remote machine, I do have services icon on desktop. When i open that, i do have services for many servers. So can you help me achieving this?
Below is my code for fetching the services from my system:
private void button1_Click(object sender, EventArgs e)
{

ServiceController[] se = ServiceController.GetDevices();
ServiceController[] services = ServiceController.GetServices();


int i = 0;

foreach (ServiceController service in services)
{
dataGridView1.Rows.Add();
service.ServiceName = "ALG"; //Finding the paticular service
dataGridView1.Rows[i].Cells["dgvcServiceName"].Value = Convert.ToString(service.ServiceName);
dataGridView1.Rows[i].Cells["dgvcStatus"].Value = Convert.ToString(service.Status);
dataGridView1.Rows[i].Cells["dgvcDescription"].Value = Convert.ToString(service.DisplayName);
i += 1;
}
Richard Deeming 24-Feb-16 9:36am    
Call the overloads of GetServices[^] and GetDevices[^] which accept the machine name.

For the local machine, pass ".". For the remote machines, pass the machine name.

Your application will need to run as an account which has permission to read the services on the remote computer.

Monitor and Manage Services on Remote Machines - CodeProject[^]

1 solution

Use WMI to know the service status
 
Share this answer
 
Comments
Member 8010354 24-Feb-16 8:36am    
Hi,

I have tried and now i'm able to get the service for my computer. Now how can i get the services from other machine (Where i used to connect remotely from my laptop). On that remote machine, I do have services icon on desktop. When i open that, i do have services for many servers. So can you help me achieving this?
Below is my code for fetching the services from my system:
private void button1_Click(object sender, EventArgs e)
{

ServiceController[] se = ServiceController.GetDevices();
ServiceController[] services = ServiceController.GetServices();


int i = 0;

foreach (ServiceController service in services)
{
dataGridView1.Rows.Add();

dataGridView1.Rows[i].Cells["dgvcServiceName"].Value = Convert.ToString(service.ServiceName);
dataGridView1.Rows[i].Cells["dgvcStatus"].Value = Convert.ToString(service.Status);
dataGridView1.Rows[i].Cells["dgvcDescription"].Value = Convert.ToString(service.DisplayName);
i += 1;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900