ServiceController shows incorrect MachineName
c#, windows-services
Solution
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);
}
Problem
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?