1

Whenever a windows service is installed/reinstalled, i want to assign the set user to the service ie. if the service is running under a user test123 with a password,i need to make sure when the installer is ran the service is attached to the same user (not to any localsystem (System.ServiceProcess.ServiceAccount.LocalSystem)).


    //Check whether my service exists- MyWinService is my service name.

     System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select Name,startname from Win32_Service where name ='MyWinService' ")); 
 using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
{
                        foreach (System.Management.ManagementObject service1 in mgmtSearcher.Get())
            {
string servicelogondetails =
string.Format("Name: {0} ,  Logon : {1} ", service1["Name"].ToString(), service1["startname"]).ToString();
MessageBox.Show(servicelogondetails).ToString();
              }
 }

I am not able to find passsword property to assign it (I don't want to view/read, just assign back).

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394418(v=vs.85).aspx

System.ServiceProcess.ServiceProcessInstaller installer=new
System.ServiceProcess.ServiceProcessInstaller(); 

installer.Account = System.ServiceProcess.ServiceAccount.User;

My question is if i assign to ServiceAccount.User, do i need to set again user name & password or if its ServiceAccount.User will take care of user name and password? If i need to assign password back to installer, how do i can do that? Thanks in advance.

installer.Password = <<something>>;
installer.Username = <<something>>;

PS: System.Management.dll is referenced to the project & using System.Management is being added to the namespace in order for System.Management.SelectQuery to work.

4

1 回答 1

0

为安装程序分配用户名和密码是可选的。如果没有通过,安装服务时会提示。

如果要分配用户名和密码,可以通过读取一些外部配置文件并分配它们来实现。

我不知道有一种方法可以从现有服务中检索用户凭据并分配它们。

于 2012-04-27T20:31:02.393 回答