C#创建带有界面交互的windows服务

C#创建windows服务默认是没有交互界面的,form和console程序均无法显示,但是可以在服务中打开桌面交互,开始>>运行>>services.msc>>选中你的windows服务>>右键点属性>>登录>>勾选允许服务与桌面交互,另外可以在服务的installer中添加AfterInstall事件响应:
   private void serviceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            SetServiceDesktopInsteract(this.serviceInstaller.ServiceName);
        }

        /// <summary>
        /// 允许服务使用界面交互
        /// </summary>
        /// <param name="serviceName"></param>
        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);
        }

原文地址:https://www.cnblogs.com/jiangdaoli/p/1755228.html