Add a Windows Service

Source: Internet
Author: User
Code
1. Install the service:
Private void installservice (idictionary statesaver, string filepath)
{
Try
{
System. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller ("servicename ");
If (! Serviceisexisted ("servicename "))
{
// Install Service
Assemblyinstaller myassemblyinstaller = new assemblyinstaller ();
Myassemblyinstaller. usenewcontext = true;
Myassemblyinstaller. Path = filepath;
Myassemblyinstaller. Install (statesaver );
Myassemblyinstaller. Commit (statesaver );
Myassemblyinstaller. Dispose ();
// -- Start service
Service. Start ();
}
Else
{
If (service. status! = System. serviceprocess. servicecontrollerstatus. Running & service. status! = System. serviceprocess. servicecontrollerstatus. startpending)
{
Service. Start ();
}
}
}
Catch (exception ex)
{
Throw new exception ("installserviceerror \ n" + ex. Message );
}
}

Ii. Uninstall the Windows Service:
Private void uninstallservice (string filepath)
{
Try
{
If (serviceisexisted ("servicename "))
{
// Uninstall service
Assemblyinstaller myassemblyinstaller = new assemblyinstaller ();
Myassemblyinstaller. usenewcontext = true;
Myassemblyinstaller. Path = filepath;
Myassemblyinstaller. Uninstall (null );
Myassemblyinstaller. Dispose ();
}
}
Catch (exception ex)
{
Throw new exception ("uninstallserviceerror \ n" + ex. Message );
}
}

3. Determine whether the window service exists:
Private bool serviceisexisted (string servicename)
{
Servicecontroller [] services = servicecontroller. getservices ();
Foreach (servicecontroller s in services)
{
If (S. servicename = servicename)
{
Return true;
}
}
Return false;
}

4. Start the service:
Private void startservice (string servicename)
{
If (serviceisexisted (servicename ))
{
System. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename );
If (service. status! = System. serviceprocess. servicecontrollerstatus. Running & service. status! = System. serviceprocess. servicecontrollerstatus. startpending)
{
Service. Start ();
For (INT I = 0; I <60; I ++)
{
Service. Refresh ();
System. Threading. thread. Sleep (1000 );
If (service. Status = system. serviceprocess. servicecontrollerstatus. Running)
{
Break;
}
If (I = 59)
{
Throw new exception (startserviceerror. Replace ("$ S $", servicename ));
}
}
}
}
}

5. Stop the service:
Private void stopservice (string servicename)
{
If (serviceisexisted (servicename ))
{
System. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename );
If (service. Status = system. serviceprocess. servicecontrollerstatus. Running)
{
Service. Stop ();
For (INT I = 0; I <60; I ++)
{
Service. Refresh ();
System. Threading. thread. Sleep (1000 );
If (service. Status = system. serviceprocess. servicecontrollerstatus. Stopped)
{
Break;
}
If (I = 59)
{
Throw new exception (stopserviceerror. Replace ("$ S $", servicename ));
}
}
}
}
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.