开发者

Confirm service credentials changed

开发者 https://www.devze.com 2023-01-22 01:21 出处:网络
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 a

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
0

精彩评论

暂无评论...
验证码 换一张
取 消