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.