如何在windows服务中打开文件


//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


发布了13 篇原创文章 · 获赞 0 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/mr_tanglin/article/details/8558320