Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string[] args;
- ManagedInstallerClass.InstallHelper(args);
- IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
- Inst.Install("MySvc", "My Sample Service", "Service that executes something",
- _InstanceID,
- // System.ServiceProcess.ServiceAccount.LocalService, // this is more secure, but only available in XP and above and WS-2003 and above
- System.ServiceProcess.ServiceAccount.LocalSystem, // this is required for WS-2000
- System.ServiceProcess.ServiceStartMode.Automatic);
- if (controller == null)
- {
- controller = new System.ServiceProcess.ServiceController(String.Format("MySvc_{0}", _InstanceID), ".");
- }
- if (controller.Status == System.ServiceProcess.ServiceControllerStatus.Running)
- {
- Start_Stop.Text = "Stop Service";
- Start_Stop_Debugging.Enabled = false;
- }
- else
- {
- Start_Stop.Text = "Start Service";
- Start_Stop_Debugging.Enabled = true;
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using Microsoft.Win32;
- namespace MySvc
- {
- class IntegratedServiceInstaller
- {
- public void Install(String ServiceName, String DisplayName, String Description,
- String InstanceID,
- System.ServiceProcess.ServiceAccount Account,
- System.ServiceProcess.ServiceStartMode StartMode)
- {
- //http://www.theblacksparrow.com/
- System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
- ProcessInstaller.Account = Account;
- System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
- System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
- string processPath = Process.GetCurrentProcess().MainModule.FileName;
- if (processPath != null && processPath.Length > 0)
- {
- System.IO.FileInfo fi = new System.IO.FileInfo(processPath);
- String path = String.Format("/assemblypath={0}", fi.FullName);
- String[] cmdline = { path };
- Context = new System.Configuration.Install.InstallContext("", cmdline);
- }
- SINST.Context = Context;
- SINST.DisplayName = String.Format("{0} - {1}", DisplayName, InstanceID);
- SINST.Description = String.Format("{0} - {1}", Description, InstanceID);
- SINST.ServiceName = String.Format("{0}_{1}", ServiceName, InstanceID);
- SINST.StartType = StartMode;
- SINST.Parent = ProcessInstaller;
- // http://bytes.com/forum/thread527221.html
- SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" };
- System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
- SINST.Install(state);
- // http://www.dotnet247.com/247reference/msgs/43/219565.aspx
- using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEMCurrentControlSetServices{0}_{1}", ServiceName, InstanceID), true))
- {
- try
- {
- Object sValue = oKey.GetValue("ImagePath");
- oKey.SetValue("ImagePath", sValue);
- }
- catch (Exception Ex)
- {
- System.Windows.Forms.MessageBox.Show(Ex.Message);
- }
- }
- }
- public void Uninstall(String ServiceName, String InstanceID)
- {
- //http://www.theblacksparrow.com/
- System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
- System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\install.log", null);
- SINST.Context = Context;
- SINST.ServiceName = String.Format("{0}_{1}", ServiceName, InstanceID);
- SINST.Uninstall(null);
- }
- }
- }
Add Comment
Please, Sign In to add comment