There are numerous examples of how to set the user logon credentials for a Windows Service however I can't discover how you first ascertain what the current credentials are set for that Windows Service.
What I want to do is:
If(WinService.logonCredentials == LocalUser)
WinService.logonCredentials = new logonCredentials;
Is there a class that I can access that will give me the required data and/or possibly other settings for the Windows Service as well?
Im not sure, but if you cant do it with classes built in .net probably only way is using WMI.
This is Win32Service class:
class Win32_Service : Win32_BaseService
{
boolean AcceptPause;
boolean AcceptStop;
string Caption;
uint32 CheckPoint;
string CreationClassName;
string Description;
boolean DesktopInteract;
string DisplayName;
string ErrorControl;
uint32 ExitCode;
datetime InstallDate;
string Name;
string PathName;
uint32 ProcessId;
uint32 ServiceSpecificExitCode;
string ServiceType;
boolean Started;
string StartMode;
string StartName;
string State;
string Status;
string SystemCreationClassName;
string SystemName;
uint32 TagId;
uint32 WaitHint;
};
And this is what you are asking:
string StartName;
I used PowerShell to get data about "Remote desktop" service on my laptop and i got a more data like this (some of that data are properties from Win32_BaseService, not Win32Service):
DesktopInteract : False
DisconnectedSessions : 1
DisplayName : Remote desktop services
ErrorControl : Normal
ExitCode : 1077
InstallDate :
Name : TermService
PathName : C:\Windows\System32\svchost.exe -k NetworkService
ProcessId : 0
ServiceSpecificExitCode : 0
ServiceType : Share Process
Started : False
StartMode : Manual
StartName : NT Authority\NetworkService
State : Stopped
Status : OK
SystemCreationClassName : Win32_ComputerSystem
SystemName : NOTEBOOK
TagId : 0
TotalSessions : 2
WaitHint : 0
I cant help with WMI in C#. Maybe you will find StartName property somewhere in class which you are using (i dont know what class it is because u didnt wrote).
Here is how to retrieve this information using WMI in C#:
public string GetWindowsServiceLoginCredentials(string serviceName)
{
var credentials = (
from x
in new ManagementObjectSearcher($"SELECT StartName FROM Win32_Service WHERE Caption = '{serviceName}'")
.Get()
.Cast<ManagementObject>()
select
x.GetPropertyValue("StartName")).FirstOrDefault();
return credentials != null ? credentials.ToString() : "";
}
You can use this to get other properties of the service, just change out the 'StartName' for the property you're interested in.
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