如何在不创建安装程序的情况下安装C#Windows服务?
有人知道有没有一种方法可以安装用C#创建的Windows服务,而无需安装程序?您可以使用installutil 从命令行:如何在不创建安装程序的情况下安装C#Windows服务?,c#,windows-services,C#,Windows Services,有人知道有没有一种方法可以安装用C#创建的Windows服务,而无需安装程序?您可以使用installutil 从命令行: installutil YourWinService.exe 此实用程序与.NET Framework一起安装您可以尝试windows C:\WINDOWS\system32>sc-create 说明: SC是一个用于与NT服务控制器和服务通信的命令行程序。我包括一个为我安装的类。我使用命令行参数调用应用程序来安装或卸载应用程序。在过去,我还向用户提供了一个提示,提示他们
installutil YourWinService.exe
此实用程序与.NET Framework一起安装您可以尝试windows
C:\WINDOWS\system32>sc-create
说明:
SC是一个用于与NT服务控制器和服务通信的命令行程序。我包括一个为我安装的类。我使用命令行参数调用应用程序来安装或卸载应用程序。在过去,我还向用户提供了一个提示,提示他们在直接从命令行启动时是否需要安装服务 以下是我使用的类:
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);
我相信您仍然需要在项目中为installutil创建一个安装程序,以便使用服务的配置属性:Dan是正确的,您仍然需要创建一个安装程序。sc命令(见下文)将允许您在不需要安装程序的情况下安装/删除/启动/停止Windows服务(对我来说,这似乎是问题的核心)。这很方便,因为烘焙到安装程序中的大部分服务元数据(启动类型、帐户名、重新启动属性等)都可以存储在外部。当与用于部署的脚本(如MSBuild或Nant)结合使用时,这将非常有用,因为它不需要重新编译。它还意味着你可以安装它是否用C,C,C++编写。你需要一个“安装程序”类,但不是一个安装程序。