0

i like to know that when we start & stop win service programmatically from another apps then how could i pass some parameter to windows service programmatically....is it possible.

here is my sample code which i use to start service programmatically.

private const int RestartTimeout = 10000;

private readonly ServiceController service;

public Control(string serviceName, string computerName)
{
    service = new ServiceController(serviceName, computerName);
}

public Control(string serviceName)
{
    service = new ServiceController(serviceName);
}

public bool StartService()
{
    try
    {
        service.Refresh();

        if (service.Status == ServiceControllerStatus.Stopped)
        {
            service.Start();
            return true;
        }

        MessageBox.Show(string.Format("{0} --> already started", service.DisplayName));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), @"Error Starting Service");
    }

    return false;
}
0

2 Answers 2

4

The ServiceController.Start() method has an overload that takes an array of strings for parameters which can be implemented further. See http://msdn.microsoft.com/en-us/library/9c38b683.aspx

Sign up to request clarification or add additional context in comments.

Comments

-3

Okay,

i think this ís possible, however, I think it can only be done in a different manner because ServiceController (and Windows Services in general) do not accept parameters.

My guess would be:

  1. Stop Service (which starts c:\program.exe "Param A")
  2. Delete Service.
  3. Add Service (which starts c:\program.exe "Param B")
  4. Start Service.

However, I'd recommend getting the parameters (or variables) into the application by using DB connection, resource files - the usual route.

Ofcouse, you could combine both paths, using a config.ini file as paramater and editing this file (once you've stopped the service).

Your application would receive an array of arguments which you can process

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.