Entwickler-Ecke

Sonstiges (.NET) - Windows Dienst aus assembly instllieren bzw. de-installieren


Soc - Do 19.11.09 12:07
Titel: Windows Dienst aus assembly instllieren bzw. de-installieren
Hallo zusammen,

ich würde gerne aus meiner Setup Methode einen Windows Dienst installieren bzw. auch de-installieren.
Laut MS soll man das Tool installutil.exe benutzen.
Ich brauche es aber als Methode in meiner Anwendung.

Hat das schon mal jemand programmiert ?

Gruß

Soc


Soc - Do 19.11.09 19:01

Hallo Zusammen,

hab was im Netz gefunden.
Funktioniert auch ganz gut.


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;

namespace snapshotGUI
{
    class ssUTIL
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Console.Title = "Dienste starten, anhalten, fortfahren und stoppen";

            string _serviceName = "ssSERV";
            string _exeName = Application.StartupPath + @"\ssSERV.exe";
            string _logName = Application.StartupPath + @"\ssSERV_Install.log";
            string _displayName = "snapshotGUI - Scheduler";

            string _description = "Dieser snapshotGUI Service triggert die einzelnen Backups";
            ServiceStartMode _startmode = ServiceStartMode.Automatic;

            if (args.Length == 0)
                Console.WriteLine("Kein Parameter übergeben");
            else
            {
                switch (args[0])
                {
                    case "install":
                        {
                            try
                            {
                                IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
                                Inst.Install(_serviceName, _displayName, _description,
                                //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
                                _startmode,
                                _exeName,
                                _logName);
                                ServiceController controller = new System.ServiceProcess.ServiceController(_serviceName);
                                break;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Fehler beim installieren des Dienst:\n\r{0}", e);
                            }
                            break;
                        }
                    case "uninstall":
                        {
                            try
                            {
                                IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
                                Inst.Uninstall(_serviceName, _logName);
                                break;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Fehler beim de-installieren des Dienst:\n\r{0}", e);
                            }
                            break;
                        }


Und dann noch folgende Klasse:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
    #region Klasse IntegratedServiceInstaller (Dienst)
    class IntegratedServiceInstaller
    {
        public void Install(String ServiceName, String DisplayName, String Description,
            System.ServiceProcess.ServiceAccount Account,
            System.ServiceProcess.ServiceStartMode StartMode, string _path, string _logpath)
        {
            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 path = String.Format("/assemblypath={0}", _path);
            String[] cmdline = { path };
            //Context = new System.Configuration.Install.InstallContext("c:\\install.log", cmdline);
            Context = new System.Configuration.Install.InstallContext(_logpath, cmdline);

            SINST.Context = Context;
            SINST.DisplayName = String.Format(DisplayName);
            SINST.Description = String.Format(Description);
            SINST.ServiceName = String.Format(ServiceName);
            SINST.StartType = StartMode;
            SINST.Parent = ProcessInstaller;
            string _exeName = Application.StartupPath + @"\ssSERV.exe";

            //SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" };

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            SINST.Install(state);

            using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + ServiceName, 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 _logpath)
        {
            System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

            //System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\\install.log", null);
            System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext(_logpath, null);
            SINST.Context = Context;
            SINST.ServiceName = ServiceName;
            SINST.Uninstall(null);
        }
    }

    #endregion


Was haltet Ihr davon ?
Ist das Teil soweit o.k. und sicher ?

Gruss

Soc