The simplest way is to use a sc command tool:
Example for changing the startup type to disabled:
sc config "MySql" start=disabled
Note you need to have the administrator privileges to run this command successfully.
Wrapping with C# code:
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "CMD.EXE",
Arguments = string.Format("/C sc {0} {1} {2}", "config", "MySql", "start=disabled"),
};
using (var process = new Process { StartInfo = startInfo})
{
if (!process.Start())
{
return;
}
process.WaitForExit();
Console.WriteLine($"Exit code is {process.ExitCode}");
}
Update:
Use process.Exit
code to check if process the operation succeeded or not.
0 ExitCode
is success.
Note: In case you are running the process/Visual Studio without the Admin privileges, the ExitCode
will be 5 (access deined).