Given a service name, I would like to retrieve the username that it runs under (i.e. the username shown in the 'Log On' tab of a service's properties window).
There doesn't appear to be anything in the ServiceController
class to retrieve this basic information. Nothing else in System.ServiceProcess
looks like it exposes this information either.
Is there a managed solution to this, or am I going to have to drop down into something lower-level?
WindowsIdentity. GetCurrent(). Name; But, it is giving the output "NT AUTHORITY\SYSTEM " as the current user name after deploying the service.
The default user account on Windows under which services install is the "Local System" account.
The Log on as a service user right allows accounts to start network services or services that run continuously on a computer, even when no one is logged on to the console. The risk is reduced because only users who have administrative privileges can install and configure services.
Answer. No, you should not have 2 services with the same name. It is mentioned in the Service Configuration Guide that service names should be unique. It it may be possible to create 2 services with the same name, but this can lead to problems in the future and is not a supported configuration.
WMI is your friend. Look at Win32_Service, specifically the StartName
property. You can access WMI from C# via the System.Management.ManagementClass.
If you've not used WMI before, this article seems to be quite a good tutorial.
Using WMI, with the System.Management you can try the following code:
using System;
namespace WindowsServiceTest
{
class Program
{
static void Main(string[] args)
{
System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select name, startname from Win32_Service")); // where name = '{0}'", "MCShield.exe"));
using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
{
foreach (System.Management.ManagementObject service in mgmtSearcher.Get())
{
string servicelogondetails =
string.Format("Name: {0} , Logon : {1} ", service["Name"].ToString(), service["startname"]).ToString();
Console.WriteLine(servicelogondetails);
}
}
Console.ReadLine();
}
}
}
You can then later substitute the commented code with your service name, and it should only return the instances of your service process that is running.
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