public void Install(String ServiceName, String DisplayName, String Description, System.ServiceProcess.ServiceAccount Account, System.ServiceProcess.ServiceStartMode StartMode) { 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); //Context = new System.Configuration.Install.InstallContext(); //Context.Parameters.Add("assemblyPath", fi.FullName); //Context.Parameters.Add("startParameters", "Test"); String path = String.Format("/assemblypath={0}", fi.FullName); String[] cmdline = { path }; Context = new System.Configuration.Install.InstallContext("", cmdline); } SINST.Context = Context; SINST.DisplayName = DisplayName; SINST.Description = Description; SINST.ServiceName = ServiceName; SINST.StartType = StartMode; SINST.Parent = ProcessInstaller; // http://bytes.com/forum/thread527221.html // SINST.ServicesDependedOn = new String[] {}; 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(@"SYSTEM\CurrentControlSet\Services\{0}", SINST.ServiceName), true)) { try { Object sValue = oKey.GetValue("ImagePath"); oKey.SetValue("ImagePath", sValue); } catch (Exception Ex) { // System.Console.WriteLine(Ex.Message); } } }
public static void InstallService(string serviceName, string displayName, string description, string assemblyPath) { System.ServiceProcess.ServiceProcessInstaller spi = new System.ServiceProcess.ServiceProcessInstaller(); spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; System.ServiceProcess.ServiceInstaller si = new System.ServiceProcess.ServiceInstaller(); si.Description = description; si.DisplayName = displayName; si.ServiceName = serviceName; si.StartType = System.ServiceProcess.ServiceStartMode.Automatic; si.Context = new System.Configuration.Install.InstallContext(); si.Context.Parameters.Add("assemblyPath", assemblyPath); si.Parent = spi; Hashtable stateSaver = new Hashtable(); si.Install(stateSaver); }
public static void Install(String ServiceName, String DisplayName, String Description, String exeName, 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(); System.IO.FileInfo fi = new System.IO.FileInfo(exeName); String path = String.Format("/assemblypath={0}", fi.FullName); String[] cmdline = { path }; Context = new System.Configuration.Install.InstallContext("", cmdline); SINST.Context = Context; SINST.DisplayName = DisplayName; SINST.Description = Description; SINST.ServiceName = ServiceName; SINST.StartType = StartMode; SINST.Parent = ProcessInstaller; // http://bytes.com/forum/thread527221.html //SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" }; SINST.ServicesDependedOn = new String[] { }; 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(@"SYSTEM\CurrentControlSet\Services\{0}", ServiceName), true)) { try { Object sValue = oKey.GetValue("ImagePath"); oKey.SetValue("ImagePath", sValue); } catch (Exception Ex) { System.Windows.Forms.MessageBox.Show(Ex.Message); } } }
public void Install(string ServiceName, string DisplayName, string Description, string InstanceID, System.ServiceProcess.ServiceAccount Account, System.ServiceProcess.ServiceStartMode StartMode) { 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; SINST.ServicesDependedOn = new string[] { "RpcSs" }; System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary(); SINST.Install(state); using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(string.Format(@"SYSTEM\CurrentControlSet\Services\{0}_{1}", ServiceName, InstanceID), true)) { try { Object sValue = oKey.GetValue("ImagePath"); oKey.SetValue("ImagePath", sValue); } catch (Exception Ex) { Trace.TraceError(Ex.Message); } } }
public static void InstallService(string serviceName, string displayName, string description, string assemblyPath) { System.ServiceProcess.ServiceProcessInstaller spi = new System.ServiceProcess.ServiceProcessInstaller(); spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; System.ServiceProcess.ServiceInstaller si = new System.ServiceProcess.ServiceInstaller(); si.Description = description; si.DisplayName = displayName; si.ServiceName = serviceName; si.StartType = System.ServiceProcess.ServiceStartMode.Automatic; si.Context = new System.Configuration.Install.InstallContext(); si.Context.Parameters.Add("assemblyPath", assemblyPath); si.Parent = spi; Hashtable stateSaver = new Hashtable(); si.Install(stateSaver); }