我制作了一个Windows服务(它正在运行),但是它在SYSTEM用户上运行,我想让它在我当前登录的用户上运行。
这是我的服务安装程序类:
[RunInstaller(true)]
class CloudManagerServiceInstaller : Installer
{
public CloudManagerServiceInstaller()
{
var serviceInstaller = new ServiceInstaller();
var serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller.Username = Environment.MachineName + "\\carl";
serviceInstaller.DisplayName = "Z";
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Z";
this.Installers.Add(serviceInstaller);
this.Installers.Add(serviceProcessInstaller);
}
}
还有我的服务基础:
class CloudManagerServiceBase : ServiceBase
{
public int i = 0;
private System.Timers.Timer _timer;
private int m = 2;
public CloudManagerServiceBase()
{
this.ServiceName = "ZSCloudManager";
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
_timer = new System.Timers.Timer(m * 60 * 1000); // every m minutes
_timer.Elapsed += _timer_Elapsed;
_timer.Start(); // <- important
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{ do work }}
我在另一个progamm的帮助下安装了该服务。 我可以写这样的服务,还是必须遵循这些说明 - &gt; http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx 如果是这样的情况那么请你给我一个代码片段,因为我不完全理解这一点。
答案 0 :(得分:1)
我放弃了手动操作,并使用Visual Studio中的serivice预设。我刚刚将它添加到预设中(在projectInstaller类中):
public ProjectInstaller()
{
InitializeComponent();
serviceProcessInstaller1.Username = ".\\" + Environment.UserName;
}
并将属性帐户从serviceProcessInstaller设置为&#34; user&#34;。之后,您可以在Service类中添加代码。它对我来说很好。