简体   繁体   中英

Change service credentials using ServiceController

Is there a way to do this, but instead of using WMI which is not currently working in my environment, using ServiceController Class.

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");
                }

Thanks!

The ServiceController class doesn't let you change service credentials. If WMI isn't an option, you can P/Invoke ChangeServiceConfig instead. You can find a C# example here:

Changing Log On Credentials of Windows Services

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM