tags:

views:

25

answers:

2

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");
                }
+4  A: 

The new credentials won't apply until you restart the service, and I suggest you use the ServiceController instead of WMI.

A_Nablsi
+1 - this is a more natural API for this purpose.
Steve Townsend
+1  A: 

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
Steve Townsend