/// <summary> /// 安装 Windows 服务 /// </summary> /// <param name="filepath">服务程序文件路径。</param> /// <param name="serviceName">服务名称。</param> /// <param name="options">在为程序集安装创建新的 System.Configuration.Install.InstallContext 对象时要使用的命令行。设置为 null 表示忽略此项。</param> public static void InstallService(string filepath, string serviceName, string[] options) { //try //{ if (!IsServiceExisted(serviceName)) { System.Collections.IDictionary mySavedState = new System.Collections.Hashtable(); using (System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller()) { myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; if (options != null) { myAssemblyInstaller.CommandLine = options; } try { myAssemblyInstaller.Install(mySavedState); myAssemblyInstaller.Commit(mySavedState); } catch { myAssemblyInstaller.Rollback(mySavedState); throw; } //myAssemblyInstaller.Dispose(); } } //} //catch (Exception ex) //{ // throw new Exception("Install Service Error\n" + ex.Message); //} }
private static void Uninstall() { if (!HasAdministrativeRight()) { Console.WriteLine("Administrative Privileges Required To Uninstall Service"); } else { Console.WriteLine("Uninstalling CloneDeploy Proxy DHCP service"); try { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(Assembly.GetExecutingAssembly(), new string[] { }); Installer.UseNewContext = true; Installer.Uninstall(null); Console.WriteLine(); Console.WriteLine("Successfully Uninstalled CloneDeploy Proxy DHCP service"); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine("Could Not Uninstall CloneDeploy Proxy DHCP"); Console.WriteLine(ex.Message); } } }
/// <summary> /// internalUninstallSnapin method implementation /// </summary> private static void internalUninstallSnapin(Session session, string dllFilename) { try { string dir = Path.GetDirectoryName(dllFilename); string file = dir + "\\" + Path.GetFileNameWithoutExtension(dllFilename) + ".installLog"; File.Delete(file); System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller(); installer.UseNewContext = true; installer.Path = dllFilename; installer.CommandLine = new string[2] { string.Format("/logFile={0}", file), string.Format("/InstallStateDir={0}", dir) }; installer.Uninstall(new Hashtable()); string state = Path.GetFileNameWithoutExtension(dllFilename) + ".installState"; File.Delete(dir + "\\" + state); } catch (Exception e) { session.Log(e.Message); throw e; } }
public System.Configuration.Install.AssemblyInstaller GetAssemblyInstaller(string location, string[] commandLineOptions) { var installer = new System.Configuration.Install.AssemblyInstaller(location, commandLineOptions); installer.UseNewContext = true; return(installer); }
private static System.Configuration.Install.AssemblyInstaller GetInstaller() { System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller( typeof(PlateletActiveService).Assembly, null); installer.UseNewContext = true; return installer; }
private static void InstallService() { var installer = new System.Configuration.Install.AssemblyInstaller(typeof(BillingServiceInstaller).Assembly, null); installer.UseNewContext = true; installer.Install(null); }
public void Run() { if (!HasAdministrativeRight()) { Console.WriteLine("Administrative Privileges Required To Install Service"); } else { Console.WriteLine("Installing Client Management"); try { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(Assembly.GetExecutingAssembly(), new string[] { }); Installer.UseNewContext = true; Installer.Install(null); Installer.Commit(null); Console.WriteLine(); Console.WriteLine("Successfully Installed Client Management"); Console.WriteLine("The Service Must Manually Be Started"); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine("Could Not Install Client Management"); Console.WriteLine(ex.Message); } } }
public static void internalInstallSnapin(string dllFilename) { if (!File.Exists(dllFilename)) { return; } System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller(); IDictionary mySavedState = new Hashtable(); string dir = Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine); string file = dir + "\\adfsmfa_snapin.log"; if (File.Exists(file)) { File.Delete(file); } installer.UseNewContext = true; installer.Path = dllFilename; installer.CommandLine = new string[2] { string.Format("/logFile={0}", file), string.Format("/InstallStateDir={0}", dir) }; mySavedState.Clear(); installer.Install(mySavedState); installer.Commit(mySavedState); }
private static void InstallService() { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(SERVICE_EXE_NAME, new string[] { }); Installer.UseNewContext = true; Installer.Install(null); Installer.Commit(null); }
public void UninstallService(string ExeFilename, string[] commandLine) { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename, commandLine); Installer.UseNewContext = true; Installer.Uninstall(null); }
protected override void DoExecute(ITaskContext context) { IDictionary savedState = new Hashtable(); string[] commandLine = new string[0]; using (System.Configuration.Install.AssemblyInstaller assemblyInstaller = new System.Configuration.Install.AssemblyInstaller(executablePath, commandLine)) { assemblyInstaller.UseNewContext = true; assemblyInstaller.Uninstall(savedState); } }
private void UninstallService() { try { System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller("SqlAuditor.Service.exe", new string[] { }); installer.UseNewContext = true; installer.Uninstall(null); installer.Commit(null); MetroMessageBox.Show(this, "SqlAuditor service successfully uninstalled.", "SqlAuditor", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MetroMessageBox.Show(this, "Error uninstalling SqlAuditor Service.\n" + ex.Message, "SqlAuditor", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void installBtn_Click(object sender, EventArgs e) { if (installUtilRb.Checked) { ServiceHelper.InstallService(utilPathTb.Text, servicePathTb.Text); } if (serviceInstallerRb.Checked) { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(servicePathTb.Text, null); Installer.UseNewContext = true; Installer.Install(null); Installer.Commit(null); } }
/// <summary> /// 安装Windows服务 /// </summary> /// <param name="servicePath">服务安装的完整路径(输入参数)</param> /// <returns>"1" : 成功;其他: 失败</returns> public static string InstallService(string servicePath) { // 参数检查 if (false == System.IO.File.Exists(servicePath)) { return("服务安装文件不存在!"); } System.IO.FileInfo Info = new System.IO.FileInfo(servicePath); System.Collections.IDictionary StateSaver = new System.Collections.Hashtable(); try { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(servicePath, new string[] { "/LogFile=" + Info.DirectoryName + @"\" + Info.Name.Substring(0, Info.Name.Length - Info.Extension.Length) + ".log" }); Installer.UseNewContext = true; Installer.Install(StateSaver); Installer.Commit(StateSaver); Type[] Types = Installer.Assembly.GetTypes(); for (int i = 0; i != Types.Length; i++) { if (Types[i].BaseType.FullName == "System.Configuration.Install.Installer") { object objA = Activator.CreateInstance(Types[i]); System.Reflection.FieldInfo[] Fields = Types[i].GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); for (int j = 0; j < Fields.Length; j++) { if (Fields[j].FieldType.FullName == "System.ServiceProcess.ServiceInstaller") { object objB = Fields[j].GetValue(objA); if (objB != null) { //serviceName = ((ServiceInstaller)objB).ServiceName; // 服务安装成功 return("1"); } } } } } return("服务安装文件不存在!"); } catch (Exception ex) { return(ex.Message); } }
private static void Uninstall() { if (!HasAdministrativeRight()) { RunElevated(Switch_Uninstall); return; } else { System.Diagnostics.Trace.WriteLine("Uninstalling DHCP service"); try { System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(Assembly.GetExecutingAssembly(), new string[] { }); Installer.UseNewContext = true; Installer.Uninstall(null); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(String.Format("Exception: {0}", ex)); } } }
/// <summary> /// 卸载 Windows 服务 /// </summary> /// <param name="filepath">服务程序文件路径。</param> /// <param name="serviceName">服务名称。</param> /// <param name="options">在为程序集安装创建新的 System.Configuration.Install.InstallContext 对象时要使用的命令行。设置为 null 表示忽略此项。</param> public static void UnInstallService(string filepath, string serviceName, string[] options) { //try //{ if (IsServiceExisted(serviceName)) { using (System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller()) { myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; if (options != null) { myAssemblyInstaller.CommandLine = options; } myAssemblyInstaller.Uninstall(null); //myAssemblyInstaller.Dispose(); } } //} //catch (Exception ex) //{ // throw new Exception("UnInstall Service Error\n" + ex.Message); //} }
/// <summary> /// 卸载Windows服务 /// </summary> /// <param name="servicePath">服务的完整路径(输入参数)</param> /// <returns>"1" : 成功;其他: 失败</returns> public static string UnInstallService(string servicePath) { // 参数检查 if (false == System.IO.File.Exists(servicePath)) { return("服务文件不存在!"); } System.IO.FileInfo Info = new System.IO.FileInfo(servicePath); System.Collections.IDictionary SavedState = new System.Collections.Hashtable(); try { System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller(servicePath, new string[] { "/LogFile=" + Info.DirectoryName + @"\" + Info.Name.Substring(0, Info.Name.Length - Info.Extension.Length) + ".log" }); installer.UseNewContext = true; installer.Uninstall(SavedState); //installer.Commit(SavedState); // 不注释会报找不到installState文件的异常 // 服务卸载成功 return("1"); } catch (Exception ex) { return(ex.Message); } }
private void okButton_Click(object sender, System.EventArgs e) { ArrayList messageList = new ArrayList(); this.Enabled = false; this.Cursor = Cursors.WaitCursor; ProgressForm progressForm = new ProgressForm(); progressForm.Owner = this; progressForm.Show(); Application.DoEvents(); string createConnectionString = null; if (this.useIntegratedSecurityCheckBox.Checked == true) { createConnectionString = "Server=" + this.databaseServerTextBox.Text + ";Integrated Security=True;"; } else { createConnectionString = "Server=" + this.databaseServerTextBox.Text + ";UID=" + this.databaseUsernameTextBox.Text + ";PWD=" + this.databasePasswordTextBox.Text + ";"; } progressForm.CurrentStepDescription = "Creating the database."; progressForm.ProgressValue = 1; Application.DoEvents(); try { ServerSetupHelper.InstallDatabase(createConnectionString, this.databaseNameTextBox.Text, this.terrariumPasswordTextBox.Text); } catch (Exception ex) { messageList.Add(ex.Message); } string webConnectionString = "Server=" + this.databaseServerTextBox.Text + ";Database=" + this.databaseNameTextBox.Text + ";UID=TerrariumUser;PWD=" + this.terrariumPasswordTextBox.Text + ";"; progressForm.CurrentStepDescription = "Preparing to create the website."; progressForm.ProgressValue = 2; Application.DoEvents(); try { ServerSetupHelper.InstallWebConfig(this.webPathText.Text, webConnectionString); } catch (Exception ex) { messageList.Add(ex.Message); } progressForm.CurrentStepDescription = "Creating the website."; progressForm.ProgressValue = 3; Application.DoEvents(); try { ServerSetupHelper.InstallWebRoot("localhost", this.webPathText.Text, this.webNameText.Text); } catch (Exception ex) { messageList.Add(ex.Message); } progressForm.CurrentStepDescription = "Setting up permissions."; progressForm.ProgressValue = 4; Application.DoEvents(); try { ServerSetupHelper.InstallACLs(this.webPathText.Text); } catch (Exception ex) { messageList.Add(ex.Message); } Application.DoEvents(); progressForm.CurrentStepDescription = "Installing performance counters and event logs."; progressForm.ProgressValue = 5; Application.DoEvents(); try { //Install the performance counters and the event logs System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller("InstallerItems.dll", null); System.Collections.Hashtable dictionary = new Hashtable(); installer.Install(dictionary); installer.Commit(dictionary); installer.Dispose(); } catch (Exception ex) { messageList.Add(ex.Message); } this.Cursor = Cursors.Default; progressForm.Close(); progressForm.Dispose(); installed = true; VerificationForm verification = new VerificationForm(messageList); verification.ServerUrl = "http://" + System.Environment.MachineName + "/" + this.webNameText.Text + "/default.aspx"; verification.Owner = this; verification.ShowDialog(); this.Close(); }
/// <summary> /// Internal task execution code. /// </summary> /// <param name="context">The script execution environment.</param> protected override void DoExecute(ITaskContext context) { string configSettingName = String.Format( System.Globalization.CultureInfo.InvariantCulture, "ServicesExist/{0}", serviceName); CheckIfServiceExistsTask checkIfServiceExistsTask = new CheckIfServiceExistsTask(serviceName, configSettingName); checkIfServiceExistsTask.Execute(context); if (context.Properties.Get <bool>(configSettingName)) { switch (mode) { case InstallWindowsServiceMode.DoNothingIfExists: return; case InstallWindowsServiceMode.FailIfAlreadyInstalled: throw new TaskExecutionException( String.Format( System.Globalization.CultureInfo.InvariantCulture, "The Windows service '{0}' already exists.", serviceName)); case InstallWindowsServiceMode.ReinstallIfExists: UninstallWindowsServiceTask uninstallWindowsServiceTask = new UninstallWindowsServiceTask(executablePath); uninstallWindowsServiceTask.Execute(context); // wait for a while to ensure the service is really deleted SleepTask.Execute(context, serviceUninstallationWaitTime); break; default: throw new NotSupportedException(); } } IDictionary savedState = new Hashtable(); string[] commandLine = new string[0]; using (System.Configuration.Install.AssemblyInstaller assemblyInstaller = new System.Configuration.Install.AssemblyInstaller(executablePath, commandLine)) { try { assemblyInstaller.UseNewContext = true; assemblyInstaller.Install(savedState); assemblyInstaller.Commit(savedState); } catch (System.ComponentModel.Win32Exception ex) { // 1073 context.WriteInfo( "ex.ErrorCode = {0}", ex.NativeErrorCode); throw; } } }
private bool UninstallSystemServic(string service,string servicePath,string commandLineOptions) { if (!System.IO.File.Exists(servicePath)) return false; if (SystemServiceExists(service)) return false; try { System.Configuration.Install.TransactedInstaller tranInstaller = new System.Configuration.Install.TransactedInstaller(); System.Configuration.Install.AssemblyInstaller assemInstaller = new System.Configuration.Install.AssemblyInstaller(servicePath, new string[] { commandLineOptions }); tranInstaller.Installers.Add(assemInstaller); System.Configuration.Install.InstallContext installContext = new System.Configuration.Install.InstallContext("install.log", new string[] { commandLineOptions }); tranInstaller.Context=installContext; tranInstaller.Uninstall(null); return true; } catch(Exception e) { ShowError(e.StackTrace.ToString()); return false; } }
private void okButton_Click(object sender, System.EventArgs e) { ArrayList messageList = new ArrayList(); this.Enabled = false; this.Cursor = Cursors.WaitCursor; ProgressForm progressForm = new ProgressForm(); progressForm.Owner = this; progressForm.Show(); Application.DoEvents(); string createConnectionString = null; if (this.useIntegratedSecurityCheckBox.Checked == true) createConnectionString = "Server=" + this.databaseServerTextBox.Text + ";Integrated Security=True;"; else createConnectionString = "Server=" + this.databaseServerTextBox.Text + ";UID=" + this.databaseUsernameTextBox.Text + ";PWD=" + this.databasePasswordTextBox.Text + ";"; progressForm.CurrentStepDescription = "Creating the database."; progressForm.ProgressValue = 1; Application.DoEvents(); try { ServerSetupHelper.InstallDatabase(createConnectionString, this.databaseNameTextBox.Text,this.terrariumPasswordTextBox.Text); } catch( Exception ex) { messageList.Add(ex.Message); } string webConnectionString = "Server=" + this.databaseServerTextBox.Text + ";Database=" + this.databaseNameTextBox.Text + ";UID=TerrariumUser;PWD=" + this.terrariumPasswordTextBox.Text + ";"; progressForm.CurrentStepDescription = "Preparing to create the website."; progressForm.ProgressValue = 2; Application.DoEvents(); try { ServerSetupHelper.InstallWebConfig(this.webPathText.Text, webConnectionString); } catch(Exception ex) { messageList.Add(ex.Message); } progressForm.CurrentStepDescription = "Creating the website."; progressForm.ProgressValue = 3; Application.DoEvents(); try { ServerSetupHelper.InstallWebRoot("localhost",this.webPathText.Text,this.webNameText.Text); } catch(Exception ex) { messageList.Add(ex.Message); } progressForm.CurrentStepDescription = "Setting up permissions."; progressForm.ProgressValue = 4; Application.DoEvents(); try { ServerSetupHelper.InstallACLs(this.webPathText.Text); } catch(Exception ex) { messageList.Add(ex.Message); } Application.DoEvents(); progressForm.CurrentStepDescription = "Installing performance counters and event logs."; progressForm.ProgressValue = 5; Application.DoEvents(); try { //Install the performance counters and the event logs System.Configuration.Install.AssemblyInstaller installer = new System.Configuration.Install.AssemblyInstaller("InstallerItems.dll", null); System.Collections.Hashtable dictionary = new Hashtable(); installer.Install(dictionary); installer.Commit(dictionary); installer.Dispose(); } catch(Exception ex) { messageList.Add(ex.Message); } this.Cursor = Cursors.Default; progressForm.Close(); progressForm.Dispose(); installed = true; VerificationForm verification = new VerificationForm(messageList); verification.ServerUrl = "http://" + System.Environment.MachineName + "/" + this.webNameText.Text + "/default.aspx"; verification.Owner = this; verification.ShowDialog(); this.Close(); }
private static void InstallService() { var installer = new System.Configuration.Install.AssemblyInstaller(typeof(LogServiceInstaller).Assembly, null); installer.UseNewContext = true; installer.Install(null); }