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);
}