/// <summary> /// 安装服务: /// </summary> /// <param name="filepath"></param> public void InstallService(string filepath) { try { string serviceName = GetServiceName(filepath); ServiceController service = new ServiceController(serviceName); if (!ServiceIsExisted(serviceName)) { //Install Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller() { UseNewContext = true, Path = filepath }; myAssemblyInstaller.Install(new Hashtable()); myAssemblyInstaller.Commit(new Hashtable()); myAssemblyInstaller.Dispose(); //--Start Service service.Start(); } else { if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending) { service.Start(); } } } catch (Exception ex) { throw new Exception("installServiceError/n" + ex.Message); } }
/// <summary> /// 安装Windows服务 /// </summary> /// <param name="stateSaver">状态集合</param> /// <param name="filepath">程序文件路径</param> public static void InstallService(IDictionary stateSaver, String filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Install(stateSaver); AssemblyInstaller1.Commit(stateSaver); AssemblyInstaller1.Dispose(); }
/// <summary> /// 卸载windows服务 /// </summary> /// <param name="filepath">获取或设置要安装的程序集的路径。</param> public static void UnInstallService(String serviceName, string filepath) { if (ServiceIsExisted(serviceName)) { AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } }
public static void InstallService() { string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; string strDir = System.IO.Path.GetDirectoryName(exePath); string filepath = strDir + "\\MPlayerWWService.exe"; IDictionary mSavedState = new Hashtable(); AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Install(mSavedState); myAssemblyInstaller.Commit(mSavedState); myAssemblyInstaller.Dispose(); }
/// <summary> /// 卸载Windows服务 /// </summary> /// <param name="filepath">程序文件路径</param> public static void UnInstallmyService(IDictionary stateSaver,string filepath) { try { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Uninstall(stateSaver); AssemblyInstaller1.Dispose(); } catch (Exception exp) { MessageBox.Show(exp.Message.ToString()); } }
/// <summary> /// 卸载windows服务: /// </summary> /// <param name="filepath"></param> public void UnInstallService(string filepath) { try { string serviceName = GetServiceName(filepath); if (ServiceIsExisted(serviceName)) { //UnInstall Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller() { UseNewContext = true, Path = filepath }; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } } catch (Exception ex) { throw new Exception("unInstallServiceError/n" + ex.Message); } }
public static string Uninstall(string filePath, string serviceName) { string msg = string.Empty; try { System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filePath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); msg = "卸载服务" + serviceName + "成功!"; } catch (Exception er) { msg = er.ToString(); } return(msg); }
static void Install(bool undo, string[] args) { try { Logger.Log(undo ? "Uninstalling ..." : "Installing ... "); using (var inst = new AssemblyInstaller(typeof(Program).Assembly, args)) { var state = new Hashtable(); inst.UseNewContext = true; try { if (undo) { inst.Uninstall(state); } else { inst.Install(state); inst.Commit(state); StartService(); } } catch (Exception ex) { try { Logger.Log(ex); inst.Rollback(state); } catch { } throw; } inst.Dispose(); } Logger.Log("... finished"); } catch (Exception ex) { Logger.Log(ex); } }
/// <summary> /// 安装windows服务 /// </summary> /// <param name="serviceName">服务名称</param> /// <param name="savedState">它用于保存执行提交、回滚或卸载操作所需的信息。</param> /// <param name="filepath">获取或设置要安装的程序集的路径。</param> public static void InstallService(String serviceName, IDictionary savedState, string filepath) { ServiceController service = new ServiceController(serviceName); if (!ServiceIsExisted(serviceName)) { AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Install(savedState); myAssemblyInstaller.Commit(savedState); myAssemblyInstaller.Dispose(); service.Start(); } else { if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending) { service.Start(); } } }
/// <summary> /// 安装服务 /// </summary> /// <param name="filePath">服务文件路径</param> /// <param name="serviceName">服务名称</param> /// <param name="options">选项</param> 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("请以管理员身份启动程序再执行此操作\n" + ex.Message); } }
/// <summary> /// 卸载服务 /// </summary> /// <param name="filepath"></param> public static void uninstallmyservice(string filepath) { try { AssemblyInstaller assemblyinstaller1 = new AssemblyInstaller(); assemblyinstaller1.UseNewContext = true; assemblyinstaller1.Path = filepath; assemblyinstaller1.Uninstall(null); assemblyinstaller1.Dispose(); } catch(Exception e) { throw new Exception("请以管理员身份启动程序再执行此操作\n" + e.Message); } }
/// <summary> /// 安装Windows服务 /// </summary> /// <param name="stateSaver">集合</param> /// <param name="filePath">程序文件路径</param> public static void InstallmyService(IDictionary stateSaver, string filePath) { var asmInstaller = new AssemblyInstaller(); asmInstaller.UseNewContext = true; asmInstaller.Path = filePath; asmInstaller.Install(stateSaver); asmInstaller.Commit(stateSaver); asmInstaller.Dispose(); }
/// <summary> /// 卸载Windows服务 /// </summary> /// <param name="filepath">程序文件路径</param> public static void UnInstallmyService(string filepath) { var asmInstaller = new AssemblyInstaller(); asmInstaller.UseNewContext = true; asmInstaller.Path = filepath; asmInstaller.Uninstall(null); asmInstaller.Dispose(); }
/// <summary> /// 卸载Windows服务 /// </summary> /// <param name="filepath">程序文件路径</param> public static void UnInstallService(String filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Uninstall(null); AssemblyInstaller1.Dispose(); }
/// <summary> /// 卸载windows服务 /// </summary> private void UnInstallService(string filepath, string serviceName) { 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\r\n" + ex.Message); } }
static void Main(string[] args) { var cancelled = false; var config = args.Where((s) => s.Contains("--config-path=")).SingleOrDefault(); if (!String.IsNullOrEmpty(config)) { var configFileName = config.Split('=')[1]; if (!String.IsNullOrEmpty(configFileName)) { _currentConfigPath = Path.Combine(Directory.GetCurrentDirectory(), configFileName); if (!File.Exists(_currentConfigPath)) throw new FileNotFoundException(String.Format("File {0} is not found in current directory", configFileName)); } } ServiceRunner runner = new ServiceRunner(args, LoadTaskHandlerDescriptors()); if (!runner.Options.IsValid) { Console.WriteLine(String.Format(runner.GetHelp(), System.IO.Path.GetFileName(Environment.GetCommandLineArgs()[0]))); return; } if (runner.Options.RunConsole) // console mode { Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; cancelled = true; Logger.Instance.Info("Exiting..."); runner.Dispose(); return; }; runner.Start(); WatchServiceAssemblies(Directory.GetCurrentDirectory(), (fname) => { // Reload the task handlers, not ready yet if (runner.Options.Mode != MSA.Zmq.JsonRpc.ServiceMode.Router) { if (Environment.UserInteractive) { PrintMessage("Reloading task handlers...", true); } runner.Reload(LoadTaskHandlerDescriptors()); } }); if (runner.Options.Mode != ServiceMode.None) while (!cancelled) { } } else { var serviceAction = runner.Options.ServiceAction; if (serviceAction == ServiceAction.Install) { // install the service with arguments AssemblyInstaller installer = new AssemblyInstaller(typeof(Program).Assembly, args); IDictionary state = new Hashtable(); try { installer.UseNewContext = true; if (runner.Options.Mode == ServiceMode.Router || runner.Options.Mode == ServiceMode.MultiWorker || runner.Options.Mode == ServiceMode.Worker || runner.Options.Mode == ServiceMode.Publisher) { // remove the --install-service arg var newArgs = args.Where(s => !s.StartsWith("--install-service")).ToArray(); installer.Installers.Add(new ZMQServiceInstaller(runner.Options.ServiceName, newArgs)); } if (serviceAction == ServiceAction.Install) { installer.Install(state); installer.Commit(state); } } catch (Exception ex) { Logger.Instance.Error(ex); installer.Rollback(state); throw; } finally { installer.Dispose(); } } else if (serviceAction == ServiceAction.Help) { Console.WriteLine(String.Format(runner.GetHelp(), System.IO.Path.GetFileName(Environment.GetCommandLineArgs()[0]))); } else // run the service { ServiceBase.Run(new ZMQService(runner)); } } }
/// <summary> /// 安装windows服务 /// </summary> private void InstallService(IDictionary stateSaver, string filepath, string serviceName) { try { ServiceController service = new ServiceController(serviceName); if (!ServiceIsExisted(serviceName)) { AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Install(stateSaver); myAssemblyInstaller.Commit(stateSaver); myAssemblyInstaller.Dispose(); service.Start(); } else { if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending) service.Start(); } } catch (Exception ex) { throw new Exception("InstallServiceError\r\n" + ex.Message); } }
//安装service private void WRegiste(string serviceName, string servicePath) { IDictionary saveStatus = new System.Collections.Hashtable(); try { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); service.Refresh(); //System.Collections.Hashtable ht = new System.Collections.Hashtable(); AssemblyInstaller r = new AssemblyInstaller(); r.UseNewContext = true; saveStatus.Clear(); r.Path = servicePath; r.Install(saveStatus); r.Commit(saveStatus); r.Dispose(); } catch (Exception ex) { MessageBox.Show("安装服务失败! " + ex.Message); } }