我们写一个服务,有时候要让服务启动某个应用程序,就要修改服务的属性,勾选允许服务与桌面交互,
可以用修改注册表实现,我们必须在安装后操作,所以请重写Installer的OnAfterInstall。
protected override void OnAfterInstall(System.Collections.IDictionary savedState) {
RegistryKey rk
= Registry.LocalMachine;
string key = @"SYSTEM\CurrentControlSet\Services\" + this.sInstaller.ServiceName;
RegistryKey sub
= rk.OpenSubKey(key, true);
int value = (int)sub.GetValue("Type");
if (value < 256) {
sub.SetValue(
"Type", value | 256);
}
base.OnAfterInstall(savedState);
}
 
但以上方法只能在重启后起到作用。。。若要想立即生效可以用以下代码代之,将此方法放入onAfterInstall即可
 
设置服务与桌面交互
private void SetServiceDesktopInsteract(string serviceName) {
ManagementObject wmiService
= new ManagementObject(string.Format("Win32_Service.Name='{0}'",serviceName));
ManagementBaseObject changeMethod
= wmiService.GetMethodParameters("Change");
changeMethod[
"DesktopInteract"] = true;
ManagementBaseObject OutParam
= wmiService.InvokeMethod("Change", changeMethod, null);
}
 
 
若想立即启动服务调System.ServiceProcess.ServiceController类来实现