c# ManagedInstallerClass.InstallHelper blocks service executable

402 Views Asked by At

I am trying to do some kind of Installer where i need to install a WinService.

What I currently do is adding the Service.exe as Resource to my Installer Project, then write all bytes of it to a specific folder.

After successfully writing the file to a specified folder, i install the service using ManagedInstallerClass.InstallHelper

The problem i've with it now is that the ManagedInstallerClass.InstallHelper is somehow locking the Service executable, so i cannot delete/overwrite the file (from outside or inside the program), because it's blocked while the Installer Application is running, beginning from first ManagedInstallerClass.InstallHelper call on the Service file.

Here is my code which causes this problem.

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

}

So it works fine at first time running it, but second time running it crashes at File.WriteAllBytes. The service is not running, Windows clearly tells me it's used by my Installer Application when trying to delete it from Windows Explorer.

I'd really appreciate an beginner(which i am) friendly explanation why this happens the way i do it and in best case a solution. I found this post but this did not worked for me.

Thanks in advice.

PS: I know there are Setup Projects and more convenient/professional things, but thats not what i need/want

1

There are 1 best solutions below

0
On

Well i am now installing it via cmd Process and sc create, still wonder why the InstallHelper wont release the file after installed successfully