您的位置:首页 > 其它

如何在windows服务中打开文件

2013-01-31 09:33 423 查看
//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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: