Windows Service Choose User or System Account on Install

半城伤御伤魂 提交于 2019-12-02 20:40:52

@Doobi, @Eric, in my experience (Win7Home 64-bit, VS2010Express, not on a domain)

 processInstaller.Account = ServiceAccount.LocalService;
 processInstaller.Username = null;
 processInstaller.Password = null;

will install the service as LocalService without a password prompt.

To install the service as a local user account (and provide a password prompt to enable the user to supply the credentials) I had to use:

 this.serviceProcessInstaller.Account =System.ServiceProcess.ServiceAccount.User;
 this.serviceProcessInstaller.Password = null;
 this.serviceProcessInstaller.Username = null;

The important step I had to take to get the service installed is to put the computer name in the credentials dialog box, ie MYPC\dave instead of dave. I was surprised that I'd have to do this as it's not on a domain. I've added this comment as no other posts I've seen about this mention having to prefix the username with the PC name.

Doobi

Yes there is, it's on the process installer. I think in the newer frameworks it's a visible property if you select the process installer on the design surface. The last time I did it (.NET 2.0) you have to add something similar to this to the *.designer.cs file:

 processInstaller.Account = ServiceAccount.LocalService;
 processInstaller.Username = null;
 processInstaller.Password = null;

Adding to previous answers, don't forget to append Machine name to Username while entering "Username" Field of password prompt. Otherwise service will not accept the credentials although if you give correct username and pwd. It will keep on pop up prompt to enter credentials. It took me one day to figure out this. Thanks to Badgerspot!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!