The problem is that you are running this code right in the class. You cannot do this, you have to run the code in a method in your class.
It looks like you want to create a service? Then put it in an
OnStart
function (but then your class has to derive from ServiceBase, too). Also in your last line, you pass "value SERVICE_ACCEPT_PRESHUTDOWN" which look like two variables. You can only pass one. I don't know exactly what you try to do, so you have to know yourself which one to pass there (but I guess it will be SERVICE_ACCEPT_PRESHUTDOWN). Also, you have to use
throw new ApplicationException
instead of
throw ApplicationException
.
class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
FieldInfo acceptedCommandsFieldInfo = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
if (acceptedCommandsFieldInfo == null)
throw new ApplicationException("acceptedCommands field not found");
int value = (int)acceptedCommandsFieldInfo.GetValue(this);
acceptedCommandsFieldInfo.SetValue(this, SERVICE_ACCEPT_PRESHUTDOWN);
}
}