//windows服务默认是不支持与桌面进行交互的,所以就无法打开硬盘上的文件,比如播放音乐等。
//需要在serviceProcessInstaller1的Committed事件中添加如下代码:
ConnectionOptions myConOptions = new ConnectionOptions(); myConOptions.Impersonation = ImpersonationLevel.Impersonate; ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", myConOptions); mgmtScope.Connect(); ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'"); ManagementBaseObject InParam = wmiService.GetMethodParameters("Change"); InParam["DesktopInteract"] = true; ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null); //在你需要打开文件的代码处添加如下代码打开文件 System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = "文件所在目录"; process.StartInfo.WorkingDirectory = "文件根目录"; process.Start();
转载于:http://www.cnblogs.com/love2wllw/archive/2010/05/15/1736339.html