private static bool RegService(RegServiceActionEnum action, object module, ServiceStartMode?startMode = null) { try { string logFileName; string stateFileName; using (AssemblyInstaller installer = new AssemblyInstaller()) { if (module is Assembly) { installer.Assembly = (Assembly)module; } else if (module is string) { installer.Path = (string)module; } else { throw new InvalidCastException("module"); } string stateDirName = Utils.GetNormalisedFullPath(installer.Path, false); logFileName = stateDirName + "\\" + Path.GetFileNameWithoutExtension(installer.Path) + ".InstallLog"; stateFileName = stateDirName + "\\" + Path.GetFileNameWithoutExtension(installer.Path) + ".InstallState"; List <string> commandLine = new List <string>(); commandLine.Add("/LogToConsole=false"); if (action == RegServiceActionEnum.Reg && startMode != null) { commandLine.Add(string.Format("/{0}={1}", RegServiceStartModeArgument, startMode.Value.ToString())); } commandLine.Add("/InstallStateDir=" + stateDirName); commandLine.Add("/LogFile=" + logFileName); installer.CommandLine = commandLine.ToArray(); installer.UseNewContext = true; Hashtable savedState = new Hashtable(); try { if (action == RegServiceActionEnum.Unreg) { installer.Uninstall(savedState); } else { installer.Install(savedState); installer.Commit(savedState); } } catch { try { installer.Rollback(savedState); } catch { } throw; } } // Если не было ошибок, то удаляем файлы журналов if (!string.IsNullOrWhiteSpace(logFileName)) { SafelyDeleteFile(logFileName); } if (!string.IsNullOrWhiteSpace(stateFileName)) { SafelyDeleteFile(stateFileName); } return(true); } catch { } return(false); }
public static bool RegService(RegServiceActionEnum action, Assembly assembly, ServiceStartMode startMode) { return(RegService(action, (object)assembly, startMode)); }
public static bool RegService(RegServiceActionEnum action, string fileName, ServiceStartMode startMode) { return(RegService(action, (object)fileName, startMode)); }
public static bool RegService(RegServiceActionEnum action, Assembly assembly) { return(RegService(action, (object)assembly, null)); }
public static bool RegService(RegServiceActionEnum action, string fileName) { return(RegService(action, (object)fileName, null)); }