2012-02-18 70 views
2

有许多关于如何为Windows服务设置用户登录凭证的示例,但是我无法发现如何首先确定为该Windows服务设置的当前凭据。如何获取Windows服务登录凭据

我想要做的是:

If(WinService.logonCredentials == LocalUser) 
    WinService.logonCredentials = new logonCredentials; 

有没有办法,我可以访问,这将使我的Windows服务所需的数据和/或其他可能的设置,以及一类?

回答

2

林不知道,但如果你不能用.net内置的类做它可能唯一的方法是使用WMI。

这是Win32Service类:

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; 
    }; 

这是你问:

 string StartName; 

我使用PowerShell来获取有关我的笔记本电脑的“远程桌面”服务数据和我有一个更多像这样的数据(其中一些数据来自Win32_BaseService,而不是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 

我不能在C#中帮助WMI。也许你会在你正在使用的类的某个地方找到StartName属性(我不知道它是什么类,因为你没有写)。

+0

这足以指向我正确的方向,特别是[链接](http://stackoverflow.com/questions/3141308/how-do-i-retrieve-the-username-that-a-windows-服务正在运行,下) – David 2012-02-18 18:43:40