简体   繁体   English

如何在不创建安装程序的情况下安装C#Windows服务?

[英]How do I install a C# Windows service without creating an installer?

有没有人知道是否有办法安装在C#中创建的Windows服务而无需安装程序?

You could try the windows sc command 你可以试试windows sc命令

C:\\WINDOWS\\system32>sc create

DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. 描述:SC是一个命令行程序,用于与NT服务控制器和服务进行通信。

I include a class that does the installation for me. 我包括一个为我安装的类。 I call the application using command line parameters to install or uninstall the app. 我使用命令行参数调用应用程序来安装或卸载应用程序。 I have also in the past included a prompt to the user whether they wanted the service installed when started directly from the command line. 我过去也曾向用户提示是否希望直接从命令行启动时安装该服务。

Here's the class I use: 这是我使用的课程:

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);
        }
    }
}

And here's how I call it: 以下是我称之为:

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);

You can use installutil . 您可以使用installutil

From the command line: 从命令行:

installutil YourWinService.exe

This utility is installed with the .NET Framework 此实用程序随.NET Framework一起安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中制作一个独立的可执行文件,该可执行文件将在没有管理员权限且用户无需执行任何操作的情况下安装 windows 服务? - How do I make a standalone executable in C# that will install a windows service without admin privileges and without the user having to do anything? Windows服务安装程序C# - Windows Service Installer C# 如何在C#中使用安装程序安装Window Service - How to install window service using installer in c# 如何使用C#查找Windows服务的安装目录? - How do I find the install directory of a Windows Service, using C#? C# - Windows服务安装程序没有注册服务 - C# - windows service installer not registering service 如何在 C# 中以编程方式安装 Windows 服务? - How to install a windows service programmatically in C#? C#Windows Service安装程序出现错误信息 - Error Message With Installer of C# Windows Service 如何使用Windows安装程序设置默认安装路径? - How do I set the default install path with a windows installer? 如何在Windows服务中手动添加安装程序? - How do I manually add an installer in a windows service? 如何使用 Visual Studio 安装程序项目安装 .NET 6 Windows 服务(Worker Service)? - How do you install a .NET 6 Windows Service (Worker Service) using a Visual Studio Installer project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM