继续使用辅助代码.。private static void InstallService(){
if (IsInstalled()) return;
try {
using (AssemblyInstaller installer = GetInstaller()) {
IDictionary state = new Hashtable();
try {
installer.Install(state);
installer.Commit(state);
} catch {
try {
installer.Rollback(state);
} catch { }
throw;
}
}
} catch {
throw;
}}private static void UninstallService(){
if ( !IsInstalled() ) return;
try {
using ( AssemblyInstaller installer = GetInstaller() ) {
IDictionary state = new Hashtable();
try {
installer.Uninstall( state );
} catch {
throw;
}
}
} catch {
throw;
}}private static void StartService(){
if ( !IsInstalled() ) return;
using (ServiceController controller =
new ServiceController("YourServiceName")) {
try {
if ( controller.Status != ServiceControllerStatus.Running ) {
controller.Start();
controller.WaitForStatus( ServiceControllerStatus.Running,
TimeSpan.FromSeconds( 10 ) );
}
} catch {
throw;
}
}}private static void StopService(){
if ( !IsInstalled() ) return;
using ( ServiceController controller =
new ServiceController("YourServiceName")) {
try {
if ( controller.Status != ServiceControllerStatus.Stopped ) {
controller.Stop();
controller.WaitForStatus( ServiceControllerStatus.Stopped,
TimeSpan.FromSeconds( 10 ) );
}
} catch {
throw;
}
}}