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 ));
}
}
}