2012-02-18 72 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