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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With