Подтвердить что ты не робот

Как установить службу С# Windows без создания установщика?

Кто-нибудь знает, есть ли способ установить службу Windows, созданную на С#, без установки установщика?

4b9b3361

Ответ 1

Вы можете использовать installutil.

Из командной строки:

installutil YourWinService.exe

Эта утилита установлена ​​с .NET Framework

Ответ 2

Вы можете попробовать окна команда sc

C:\WINDOWS\system32>sc create

ОПИСАНИЕ: SC - это программа командной строки, используемая для связи с NT Service Controller и службами.

Ответ 3

Я включаю класс, который выполняет установку для меня. Я вызываю приложение с помощью параметров командной строки для установки или удаления приложения. В прошлом я также включал запрос пользователю, хочет ли он, чтобы служба была установлена ​​при запуске непосредственно из командной строки.

В этом классе я использую:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;

namespace [your namespace here]
{
    class IntegratedServiceInstaller
    {
        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 void Uninstall(String ServiceName)
        {
            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 = ServiceName;
            SINST.Uninstall(null);
        }
    }
}

И вот как я это называю:

const string serviceName = "service_name";
const string serviceTitle = "Service Title For Services Control Panel Applet";
const string serviceDescription = "A longer description of what the service does.  This is used by the services control panel applet";
// Install
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Install(serviceName, serviceTitle, serviceDescription,
    // 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);
// Uninstall
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Uninstall(serviceName);