设置C# Windows 服务与桌面交互
2015-07-25 18:04
615 查看
static public void ServiceDesktopPermission()
{
try
{
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
// CIMV2 is a namespace that contains all of the core OS and hardware classes.
// CIM (Common Information Model) which is an industry standard for describing
// data about applications and devices so that administrators and software
// management programs can control applications and devices on different
// platforms in the same way, ensuring interoperability across a network.
ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + "Service1" + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
wmiService.InvokeMethod("Change", InParam, null);
}
catch (Exception ex)
{
//TODO: Log this error
StreamWriter sw;
sw = File.AppendText("C:\\Sashidhar.txt");
sw.WriteLine("Trying to open Service");
sw.Close();
}
}
{
try
{
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
// CIMV2 is a namespace that contains all of the core OS and hardware classes.
// CIM (Common Information Model) which is an industry standard for describing
// data about applications and devices so that administrators and software
// management programs can control applications and devices on different
// platforms in the same way, ensuring interoperability across a network.
ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + "Service1" + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
wmiService.InvokeMethod("Change", InParam, null);
}
catch (Exception ex)
{
//TODO: Log this error
StreamWriter sw;
sw = File.AppendText("C:\\Sashidhar.txt");
sw.WriteLine("Trying to open Service");
sw.Close();
}
}
相关文章推荐
- C# 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节
- C#学习笔记(十六):Attribute
- c# 多线程 调用带参数函数
- C#生成随机数
- C#中Split用法
- C#字母与ASCII码的转换
- C# 实现Tree,包含parentId和children
- C# using 三种使用方式
- C#中的反射
- C#中对泛型List进行分组输出元素
- C#实现WinForm随WINDOWS服务一起启动
- C#调用Rar文件及获取Rar返回值的方法
- C#群发邮件
- [转]学习C#:Attribute与Property
- 【转】C#中对IDisposable接口的理解
- 挖一挖C#中那些我们不常用的东西之系列(5)——FlagAttribute
- 看看C# 6.0中那些语法糖都干了些什么(中篇)
- 看看C# 6.0中那些语法糖都干了些什么(上篇)
- C# KeyDown KeyPress KeyUp 处理相应的键盘事件
- 走近,C#