C# .NET VS Setup Project, file added to Program Files (x86) gone after user switch

308 Views Asked by At

I have a C# .NET project and a VS Setup Project to install it. Inside the C# .NET project I have System.Configuration.Install.Installer subclass. The Installer subclass needs to write a file inside the "c:\Program Files (x86)\AppName" folder and this file will have content from a parameter asked for in the installer. This all works great until the user decides to log out and another user logs on. Then the file disappears and the program does not work. I need the file to be saved in the app's Program Files (x86) folder permanently and it should not be removed.

I read that this is related to Windows Vista and later have something called a Virtual Store for Program Files but I don't understand how I can bypass this so I can really place a file there. In most cases, it would be incorrect to store in this directory, but in this case, I really need to. It is a setting made in the Installer and should be there. It is the correct place.

Below is the code of the Installer class

EDIT: Now I just found something I did not notice before. Seem like when I log in to the other user, the installer is executed again immediately after login, but now there is no Context.Parameters["ActivationCode"] this time. IS this expected? What is actually starting the installer? Does it do this for every user? Since it fails I guess it will rollback and that removes the files.

public partial class ProjectInstaller : System.Configuration.Install.Installer {
    public ProjectInstaller() {
        InitializeComponent();
    }


    public override void Install(IDictionary stateSaver) {
        base.Install(stateSaver);

        if (string.IsNullOrWhiteSpace(Context.Parameters["ActivationCode"])) {
            throw new InstallException("Missing activation code!");
        }

        string installDir = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
        File.WriteAllText(installDir + "\\ActivationCode.txt", Context.Parameters["ActivationCode"]);
    }

    public override void Uninstall(IDictionary savedState) {
        base.Uninstall(savedState);

        try {
            string installDir = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
            File.Delete(installDir + "\\ActivationCode.txt");
        }
        catch (Exception) {
        }

    }

    public override void Rollback(IDictionary savedState) {
        base.Rollback(savedState);

        try {
            string installDir = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
            File.Delete(installDir + "\\ActivationCode.txt");
        }
        catch (Exception) {
        }
    }

}
0

There are 0 best solutions below