Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a class I use when writing services. I usually have an interactive screen that comes up when the service is not called. From there I use the class as needed. It allows for multiple named instances on the same machine -hence the InstanceID field</p> <p>Sample Call</p> <pre><code> 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; } </code></pre> <p>The class itself</p> <pre><code>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 &amp;&amp; processPath.Length &gt; 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(@"SYSTEM\CurrentControlSet\Services\{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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload