我需要使用 C# 更改现有 Windows 服务的凭据。我知道有两种不同的方法可以做到这一点。
- 更改ServiceConfig,参见pinvoke.net 上的 ChangeServiceConfig http://www.pinvoke.net/default.aspx/advapi32.ChangeServiceConfig
- ManagementObject.InvokeMethod 使用 Change 作为方法名称。
这似乎都是一种非常“友好”的方式,我想知道我是否错过了另一种更好的方式来做到这一点。
这是一种使用 System.Management 类的快速但肮脏的方法。
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace ServiceTest
{
class Program
{
static void Main(string[] args)
{
string theServiceName = "My Windows Service";
string objectPath = string.Format("Win32_Service.Name='{0}'", theServiceName);
using (ManagementObject mngService = new ManagementObject(new ManagementPath(objectPath)))
{
object[] wmiParameters = new object[11];
wmiParameters[6] = @"domain\username";
wmiParameters[7] = "password";
mngService.InvokeMethod("Change", wmiParameters);
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)