Install, start, and stop uninstall Windows Service C #

Source: Internet
Author: User

Problem: An error occurred while installing Windows Services: system. componentmodel. win32exception: the account name is invalid or does not exist,

Solution: Set serviceprocessinstaller1-> accout attribute to LocalSystem (default: user ).

Run: installuitlProgramName: .exe. The installation is successful.

The uninstall is installuitl/u program name .exe.

 

Q: How can I stop uninstalling Windows Services without installing and starting installutil?

Solution: Use the system. configuration. Install. assemblyinstaller class to load an assembly and run the installer.
[C #]
// Install the service
Public static void installservice (string filepath, string servicename, string [] options)
{
Try
{
If (! Isserviceexisted (servicename ))
{
Idictionary mysavedstate = new hashtable ();
Assemblyinstaller myassemblyinstaller = new assemblyinstaller ();
Myassemblyinstaller. usenewcontext = true;
Myassemblyinstaller. Path = filepath;
Myassemblyinstaller. CommandLine = options;
Myassemblyinstaller. Install (mysavedstate );
Myassemblyinstaller. Commit (mysavedstate );
Myassemblyinstaller. Dispose ();
}
}
Catch (exception ex)
{
Throw new exception ("Install service error \ n" + ex. Message );
}
}
// Uninstall the service
Public static void uninstallservice (string filepath, string servicename, string [] options)
{
Try
{
If (isserviceexisted (servicename ))
{
Assemblyinstaller myassemblyinstaller = new assemblyinstaller ();
Myassemblyinstaller. usenewcontext = true;
Myassemblyinstaller. Path = filepath;
Myassemblyinstaller. CommandLine = options;
Myassemblyinstaller. Uninstall (null );
Myassemblyinstaller. Dispose ();
}
}
Catch (exception ex)
{
Throw new exception ("uninstall service error \ n" + ex. Message );
}
}
// Determine whether the service exists
Public static bool isserviceexisted (string servicename)
{
Servicecontroller [] services = servicecontroller. getservices ();
Foreach (servicecontroller s in services)
{
If (S. servicename = servicename)
{
Return true;
}
}
Return false;
}
// Start the service
Public static void startservice (string servicename)
{
If (isserviceexisted (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 ();
Service. waitforstatus (servicecontrollerstatus. Running, timespan. fromseconds (60 ));
}
}
}
// Stop the service
Public static void stopservice (string servicename)
{
If (isserviceexisted (servicename ))
{
System. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename );
If (service. Status = system. serviceprocess. servicecontrollerstatus. Running)
{
Service. Stop ();
Service. waitforstatus (servicecontrollerstatus. Stopped, timespan. fromseconds (60 ));
}
}
}

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.