<p>I need to change the credentials of an already existing Windows service using C#. I am aware of two different ways of doing this.</p>

<ol>
<li>ChangeServiceConfig, see <a href="http://www.pinvoke.net/default.aspx/advapi32.ChangeServiceConfig" rel="nofollow noreferrer">ChangeServiceConfig on pinvoke.net</a></li>
<li>ManagementObject.InvokeMethod using Change as the method name.</li>
</ol>

<p>Neither seems a very "friendly" way of doing this and I was wondering if I am missing another and better way to do this.</p>

Here is one quick and dirty method using the System.Management classes.
```
```
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);
      }
    }
  }
}
```
```