我创建了一个windows服务。我想从这个服务中打开一些基于windows的应用程序。
但是我的windows服务无法启动桌面应用程序。为了启用访问,我必须执行以下步骤:
"Services"
在此之后,我的服务可以打开所需的基于windows的进程。
我是否可以在代码(C#)中配置我的windows服务以访问桌面,以便在安装后不必手动更改访问权限?
发布于 2009-12-22 10:50:09
在.NET中,您可以重写服务安装程序类的OnCommited方法,以配置服务以访问桌面。守则如下:
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceProcessInstaller serviceProcessInstaller;
private ServiceInstaller serviceInstaller;
public ProjectInstaller()
{
InitializeComponent();
// adjust configuration to whatever is needed
serviceInstaller = new ServiceInstaller();
serviceInstaller.ServiceName = "My Service";
serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Manual;
this.Installers.Add(serviceInstaller);
serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
serviceProcessInstaller.Password = null;
serviceProcessInstaller.Username = null;
this.Installers.Add(serviceProcessInstaller);
}
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
// The following code sets the flag to allow desktop interaction
// for the service
//
using (RegistryKey ckey =
Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Services\My Service", true))
{
if (ckey != null && ckey.GetValue("Type") != null)
{
ckey.SetValue("Type", (((int)ckey.GetValue("Type")) | 256));
}
}
}
}发布于 2009-12-22 11:09:03
只是..。别。那不是服务的工作。对于这项工作,您应该使用一个用户应用程序(可能在他们的启动阶段),这个应用程序(如果需要的话)可以通过IPC与服务对话。我相信我们的计划是让用户界面在某一时刻无法从服务中获得(Vista?我很久以前就停止做service<=>desktop了。
出于考虑:
如果有多个用户登录(如果有多个RDP会话,则为快速用户switching)?
如果您认为“会话0”是为某些系统上的管理人员保留的(因此交互式用户不一定在会话0上),那么您所建议的内容实际上只会扩展到1,而可能不会发生这样的情况。
https://stackoverflow.com/questions/1945529
复制相似问题