Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保持;“允许服务与桌面交互”;在安装windows服务时默认选中_C#_Visual Studio 2012_Windows Services_.net 4.5 - Fatal编程技术网

C# 如何保持;“允许服务与桌面交互”;在安装windows服务时默认选中

C# 如何保持;“允许服务与桌面交互”;在安装windows服务时默认选中,c#,visual-studio-2012,windows-services,.net-4.5,C#,Visual Studio 2012,Windows Services,.net 4.5,我已经创建了一个windows服务,它应该打开并运行ABC.exe应用程序。我知道这样做是不好的,不要因为这个原因投反对票,因为我现在没有其他选择。我对此做了很多研究,并找到了一个使其具有互动性的解决方法。我使用了以下链接: 但我现在面临的问题是,用户必须手动设置本地系统帐户,并选中“允许服务与桌面交互”框。我现在要做的就是在安装时将其设置为默认选中状态。因此,当用户安装服务时,应该已经对其进行了检查,并且该服务应该处于运行状态(不必手动启动该服务,我已完成此部分)。我在这里看到过一些类似的帖子

我已经创建了一个windows服务,它应该打开并运行ABC.exe应用程序。我知道这样做是不好的,不要因为这个原因投反对票,因为我现在没有其他选择。我对此做了很多研究,并找到了一个使其具有互动性的解决方法。我使用了以下链接: 但我现在面临的问题是,用户必须手动设置本地系统帐户,并选中“允许服务与桌面交互”框。我现在要做的就是在安装时将其设置为默认选中状态。因此,当用户安装服务时,应该已经对其进行了检查,并且该服务应该处于运行状态(不必手动启动该服务,我已完成此部分)。我在这里看到过一些类似的帖子,但它们都展示了从外部应用程序启动服务的方法,比如创建另一个控制台应用程序和使用system.management.managementobject。我想知道是否有办法在服务本身中添加一些代码,以及在何处添加代码? 注:我正在为windows vista+系统创建此服务


请帮助我,因为我在这个问题上被困了很长一段时间。提前感谢。

我建议您直接在注册表中修改值

在installer类中,重写OnCommitted(System.Collections.IDictionary savedState)方法

在该方法中,修改确保此设置的注册表项,如下所示:

protected override void OnCommitted(System.Collections.IDictionary savedState)
{
  base.OnCommitted(savedState);
  string regKey = "SYSTEM\\CurrentControlSet\\Services\\MyServiceName";
  var key = Registry.LocalMachine.OpenSubKey(regKey, RegistryKeyPermissionCheck.ReadWriteSubTree)
          ?? Registry.LocalMachine.CreateSubKey(regKey, RegistryKeyPermissionCheck.ReadWriteSubTree);

 // might need to check the result, should be hex 110 or Decimal 272
 key.SetValue("Type", 272, RegistryValueKind.DWord); 
}

您的服务是如何安装的?使用
ServiceInstaller
类?是。我有一个项目安装程序类,其中有一个名为afterinstall的方法,在该方法中我编写了自动启动它的代码。重复的?我不知道.NET服务安装程序类是否为此提供了一种机制,但如果没有,您可以P/调用ChangeServiceConfig()。