Kel*_*sey 208

在服务项目中执行以下操作:

  1. 在解决方案资源管理器中,双击您的服务.cs文件.它应该显示一个全灰色的屏幕,并讨论从工具箱中拖动内容.
  2. 然后右键单击灰色区域并选择添加安装程序.这会将安装程序项目文件添加到项目中.
  3. 然后,您将在ProjectInstaller.cs(serviceProcessInstaller1和serviceInstaller1)的设计视图中拥有2个组件.然后,您应该根据需要设置属性,例如服务名称和应该运行的用户.

现在你需要进行一个安装项目.最好的办法是使用设置向导.

  1. 右键单击您的解决方案并添加一个新项目:添加>新建项目>设置和部署项目>设置向导

    一个.对于不同版本的Visual Studio,这可能略有不同.湾 Visual Studio 2010位于:安装模板>其他项目类型>设置和部署> Visual Studio Installer

  2. 在第二步选择"为Windows应用程序创建安装程序".

  3. 在第3步,选择"主要输出..."
  4. 单击"完成".

接下来编辑安装程序以确保包含正确的输出.

  1. 右键单击解决方案资源管理器中的安装项目.
  2. 选择查看>自定义操作.(在VS2008中,它可能是视图>编辑器>自定义操作)
  3. 右键单击"自定义操作"树中的"安装"操作,然后选择"添加自定义操作..."
  4. 在"在项目中选择项目"对话框中,选择"应用程序文件夹"并单击"确定"
  5. 单击"确定"以选择"主输出..."选项.应该创建一个新节点.
  6. 对提交,回滚和卸载操作重复步骤4 - 5.

您可以通过右键单击解决方案中的Installer项目来编辑安装程序输出名称,然后选择"属性".将"输出文件名:"更改为您想要的任何内容.通过选择安装项目以及并查看属性窗口,您可以编辑Product Name,Title,Manufacturer,等...

接下来构建安装程序,它将生成一个MSI和一个setup.exe.选择要用于部署服务的任何一种.

  • @El Ronnoco,我在发布前很久就得到了答案.我想在这里记录它,因为我总是需要每隔6到12个月查找一次(并且它并不那么容易找到)所以现在我可以轻松地搜索每个人,我可以很快找到它:) (37认同)
  • @Christopher Painter我一直在使用MS安装程序from2k5,它从来没有出现过问题.你是否同意它并认为它是一个"反模式"不是这个问题的重点,它是如何用y做x,而不是我如何做b.当我发布问题时,它是出于文档目的. (7认同)
  • 对于 VS 2019,您必须先下载此 https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects (4认同)
  • 然后你已经幸运了6年,你只是不知道它.您可能需要阅读:http://robmensching.com/blog/posts/2007/4/19/Managed-Code-CustomActions-no-support-on-the-way-and-heres (2认同)

小智 48

我按照Kelsey的第一组步骤将安装程序类添加到我的服务项目中,但是我没有创建MSI或setup.exe安装程序,而是让服务自行安装/卸载.这里有一些我可以用作起点的服务的示例代码.

public static int Main(string[] args)
{
    if (System.Environment.UserInteractive)
    {
        // we only care about the first two characters
        string arg = args[0].ToLowerInvariant().Substring(0, 2);

        switch (arg)
        {
            case "/i":  // install
                return InstallService();

            case "/u":  // uninstall
                return UninstallService();

            default:  // unknown option
                Console.WriteLine("Argument not recognized: {0}", args[0]);
                Console.WriteLine(string.Empty);
                DisplayUsage();
                return 1;
        }
    }
    else
    {
        // run as a standard service as we weren't started by a user
        ServiceBase.Run(new CSMessageQueueService());
    }

    return 0;
}

private static int InstallService()
{
    var service = new MyService();

    try
    {
        // perform specific install steps for our queue service.
        service.InstallService();

        // install the service with the Windows Service Control Manager (SCM)
        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
    }
    catch (Exception ex)
    {
        if (ex.InnerException != null && ex.InnerException.GetType() == typeof(Win32Exception))
        {
            Win32Exception wex = (Win32Exception)ex.InnerException;
            Console.WriteLine("Error(0x{0:X}): Service already installed!", wex.ErrorCode);
            return wex.ErrorCode;
        }
        else
        {
            Console.WriteLine(ex.ToString());
            return -1;
        }
    }

    return 0;
}

private static int UninstallService()
{
    var service = new MyQueueService();

    try
    {
        // perform specific uninstall steps for our queue service
        service.UninstallService();

        // uninstall the service from the Windows Service Control Manager (SCM)
        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
    }
    catch (Exception ex)
    {
        if (ex.InnerException.GetType() == typeof(Win32Exception))
        {
            Win32Exception wex = (Win32Exception)ex.InnerException;
            Console.WriteLine("Error(0x{0:X}): Service not installed!", wex.ErrorCode);
            return wex.ErrorCode;
        }
        else
        {
            Console.WriteLine(ex.ToString());
            return -1;
        }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 在生产计算机上安装时,请记住以管理员身份运行它.我创建了一个BAT文件,该文件使用/ i参数调用EXE文件,但它在生产环境中不起作用,即使我以管理员身份执行了BAT文件.我必须以管理员身份打开命令行提示符并显式调用EXE文件/ i(不使用BAT文件).至少在Windows Server 2012上发生过这种情况. (4认同)
  • @Christopher - 我没有.我的解决方案不能替代您用于分发软件的完整安装程序.我提出了另一种适用于某些情况的选项,例如我在那里编写可以在无人值守的信息亭中驱动嵌入式PC的软件. (3认同)

Ale*_*hov 24

Nor Kelsey和Brendan解决方案在Visual Studio 2015社区中对我不起作用.

以下是我使用安装程序创建服务的简要步骤:

  1. 运行Visual Studio,转到 File->New->Project
  2. 选择".NET Framework 4",在"搜索已安装的模板"中键入"服务"
  3. 选择"Windows服务".输入名称和位置.按OK.
  4. 双击Service1.cs,右键单击设计器并选择"添加安装程序"
  5. 双击ProjectInstaller.cs.对于serviceProcessInstaller1,打开"属性"选项卡,将"帐户"属性值更改为"LocalService".对于serviceInstaller1,更改"ServiceName"并将"StartType"设置为"Automatic".
  6. 双击serviceInstaller1.Visual Studio创建serviceInstaller1_AfterInstall事件.编写代码:

    private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {
        using (System.ServiceProcess.ServiceController sc = new 
        System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName))
        {
            sc.Start();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  7. 构建解决方案 右键单击项目,然后选择"在文件资源管理器中打开文件夹".转到bin\Debug.

  8. 使用以下脚本创建install.bat:

    :::::::::::::::::::::::::::::::::::::::::
    :: Automatically check & get admin rights
    :::::::::::::::::::::::::::::::::::::::::
    @echo off
    CLS 
    ECHO.
    ECHO =============================
    ECHO Running Admin shell
    ECHO =============================
    
    :checkPrivileges 
    NET FILE 1>NUL 2>NUL
    if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 
    
    :getPrivileges 
    if '%1'=='ELEV' (shift & goto gotPrivileges)  
    ECHO. 
    ECHO **************************************
    ECHO Invoking UAC for Privilege Escalation 
    ECHO **************************************
    
    setlocal DisableDelayedExpansion
    set "batchPath=%~0"
    setlocal EnableDelayedExpansion
    ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
    ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
    "%temp%\OEgetPrivileges.vbs" 
    exit /B 
    
    :gotPrivileges 
    ::::::::::::::::::::::::::::
    :START
    ::::::::::::::::::::::::::::
    setlocal & pushd .
    
    cd /d %~dp0
    %windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /i "WindowsService1.exe"
    pause
    
    Run Code Online (Sandbox Code Playgroud)
  9. 创建uninstall.bat文件(将pen-ult行更改/i/u)
  10. 要安装并启动服务,请运行install.bat,以停止和卸载运行uninstall.bat


小智 10

对于VS2017,您需要添加"Microsoft Visual Studio 2017安装程序项目"VS扩展.这将为您提供其他Visual Studio Installer项目模板.https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.MicrosoftVisualStudio2017InstallerProjects#overview

要安装Windows服务,您可以添加新的安装向导类型项目,并按照Kelsey的答案中的步骤进行操作/sf/answers/631477521/