<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Starting a windows service from .net</Title>
      <Shortcut>Startingawindowsservicefrom.net</Shortcut>
      <Description>Starting a windows service from .net [C#]</Description>
      <Author>Poovannan Rajendran</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=d412b5f3-14e9-4b2e-8c6c-9840baad35da</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[ //Retreive the Service and Machine name from config file
            string servicename = ConfigurationManager.AppSettings["ServiceMachine"].ToString();
            string serviceMachine = ConfigurationManager.AppSettings["MachineName"].ToString();
            //Create a new ServiceController object with the service and machine name as parameters
            ServiceController svc = new ServiceController(servicename, serviceMachine);
            //Start the service if its not already running
            if (svc.Status == ServiceControllerStatus.Stopped)
            {
                //Start the Service
                svc.Start();
            }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>