gpt4 book ai didi

c# - 以编程方式安装 Windows 服务

转载 作者:IT王子 更新时间:2023-10-29 03:54:40 25 4
gpt4 key购买 nike

如何在不使用 installutil.exe 的情况下以编程方式安装 Windows 服务?

最佳答案

您可以通过添加此代码(在程序文件 Program.cs 中)来安装该服务,以便在使用指定参数从命令行运行时自行安装:

/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{

if (args.Length > 0)
{
switch (args[0])
{
case "-install":
{
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
}
case "-uninstall":
{
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService() };
ServiceBase.Run(ServicesToRun);
}
}

关于c# - 以编程方式安装 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2072288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com