Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceController shows incorrect MachineName

I have the following Code

    private DataTable GetServices(string[] serviceNames)
    {
        DataTable dt = new DataTable("Services");
        dt.Columns.Add("MachineName", typeof(string));
        dt.Columns.Add("ServiceName", typeof(string));
        dt.Columns.Add("ServiceStatus", typeof(string));

        ServiceController[] services = ServiceController.GetServices();
        foreach (ServiceController scTemp in services)
        {
            if (serviceNames.Contains(scTemp.DisplayName))
            {

                dt.Rows.Add(scTemp.MachineName, scTemp.DisplayName, scTemp.Status);
            }
        }
        return dt;
    }

It returns the following

MachineName,ServiceName,ServiceStatus
.,Adobe Flash Player Update Service,Stopped
.,Application Experience,Running
.,Application Layer Gateway Service,Stopped
.,Application Host Helper Service,Running

scTemp.MachineName returns a .

How can i get it to return the real computer name?

like image 583
Josef Van Zyl Avatar asked Feb 21 '23 06:02

Josef Van Zyl


1 Answers

The "." indicates the local computer. To use the real MachineName you can use the property MachineName of the class Environment.

To solve your problem you need to add a custom mapping if the MachineName of the ServiceController returns ".".

if (scTemp.MachineName.Equals(".")) {
  dt.Rows.Add(Environment.MachineName, scTemp.DisplayName, scTemp.Status);
}
else {
  dt.Rows.Add(scTemp.MachineName, scTemp.DisplayName, scTemp.Status);
}
like image 65
Jehof Avatar answered Mar 05 '23 01:03

Jehof