您的位置:首页 > 编程语言 > C#

设置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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: