Yes, this is possible. (However, I wonder why? It's better be avoided. However, I understand that is some situations you may need it.)
First, please see my past answer with
very brief overview of installation:
How to install windows service programmatically[
^].
Basically, to make you service installable you need to implement two classes based on
System.ServiceProcess.ServiceProcessInstaller
and
System.ServiceProcess.ServiceInstaller
, please see:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx[
^].
In both of the two MDSN help pages referenced above, you will find code samples showing how to write the implementation of the installer.
Now, look at the class
System.ServiceProcess.ServiceProcessInstaller
and pay attention for its properties:
Account
,
Username
and
Password
. This is how you can customize the account.
In your case, you should use
System.ServiceProcess.User
for the
Account
property value, please see:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx[
^].
Note that the solutions with the account and password is the inherent security problem. Hard-coding of the account and password in the code is potentially unsafe. You could devise some secure mechanism for obtaining the password just once and removing it, but the installation itself will result in storing the password in the system. It's hard to give you an ultimate advice for solution of this problem, but this is just something you should understand.
I would simply create some account with insecure password and limit the permissions of these account to minimum barely required just to run the service. In particular, deny access to any disk volumes except one volume and one directory (or so) only need to run the service.
—SA