I am using the following code to change the credentials of a windows service. Before I display the message that credentials were successfully changed, I want to confirm the new credentials have been applied. How can I do that?
using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
{
object[] wmiParams = new object[11];
wmiParams[6] = _username;
开发者_如何转开发 wmiParams[7] = _password;
service.InvokeMethod("Change", wmiParams);
Thread.Sleep(2000);
//check if new credentials in order
//Console.WriteLine("Service credentials changed");
}
The new credentials won't apply until you restart the service, and I suggest you use the ServiceController instead of WMI.
You should be able to check the returned object from InvokeMethod
and just handle errors without any further complexity. The only issue is working out what returned value implies success.
object result = service.InvokeMethod("Change", wmiParams);
// if result 'is bad', handle error
精彩评论