c#ManagedInstallerClass.InstallHelper阻止服务可执行文件

c#ManagedInstallerClass.InstallHelper阻止服务可执行文件,c#,service,installation,locked-files,C#,Service,Installation,Locked Files,我正在尝试进行某种安装程序,其中需要安装WinService 我当前要做的是将Service.exe作为资源添加到我的安装程序项目中,然后将其所有字节写入特定文件夹 成功地将文件写入指定文件夹后,我使用ManagedInstallerClass.InstallHelper安装该服务 我现在遇到的问题是ManagedInstallerClass.InstallHelper以某种方式锁定了服务可执行文件,因此我无法(从程序外部或内部)删除/覆盖该文件,因为它在安装程序应用程序运行时被阻止,从服务文件

我正在尝试进行某种安装程序,其中需要安装WinService

我当前要做的是将Service.exe作为资源添加到我的安装程序项目中,然后将其所有字节写入特定文件夹

成功地将文件写入指定文件夹后,我使用ManagedInstallerClass.InstallHelper安装该服务

我现在遇到的问题是ManagedInstallerClass.InstallHelper以某种方式锁定了服务可执行文件,因此我无法(从程序外部或内部)删除/覆盖该文件,因为它在安装程序应用程序运行时被阻止,从服务文件上的第一个ManagedInstallerClass.InstallHelper调用开始

这是导致此问题的代码

private void button1_Click(object sender, EventArgs e)
{

    try
    {            
        UninstallAndStopServiceIfExist("RDPBFP_Service");
    }
    catch (Exception)
    {
        MessageBox.Show("Service not running, so cannot stop it!");
    }
    Thread.Sleep(1000);
    //File.Delete(@"C:\RDPBFP\RDP-Bruteforce-Protector_Service.exe");
    Directory.CreateDirectory(@"C:\RDPBFP");
    File.WriteAllBytes(@"C:\RDPBFP\RDPBFP_Service.exe", Properties.Resources.RDPBFP_Service);            

    try
    {
        ManagedInstallerClass.InstallHelper(new string[] { @"C:\RDPBFP\RDPBFP_Service.exe" });

    }
    catch (Exception i_ex)
    {
        MessageBox.Show(i_ex.Message);
    }


    //StartService("RDPBFP_Service");

}
所以它在第一次运行时工作正常,但第二次运行时在File.writealBytes处崩溃。 该服务未运行,Windows在试图从Windows资源管理器中删除它时明确告诉我安装程序应用程序正在使用该服务

我非常感谢初学者(我是)友好的解释,为什么我会这样做,最好的解决方案。我发现,但这对我不起作用

谢谢你的建议


PS:我知道有安装项目和更方便/专业的东西,但这不是我需要/想要的

好吧,我现在通过cmd进程和sc create安装它,仍然想知道为什么InstallHelper在成功安装后不会发布该文件