C# Setup Project Custom Action to save the installation directory to custom setting file

柔情痞子 提交于 2019-12-12 03:23:53

问题


I am trying to make an application using windows installer as it's install method. When the installation succeeds I want to get the path where the primary output is located that I configured in the installer. In my case the primary output is in the folder [ApplicationData(Installroot)]\Bin\.

I also have a custom settings file called App.Settings in 1 my Class Library which controls several settings like file locations who are relative to the install location.

So the idea is that when the installation succeeds it should call the App.Settings and save the install folder to the settings file.

I already made an Install class and put it inside the Class Library. I am not sure if it is supposed to be in that project though. This is the code of the Install Class:

using System.Collections;
using System.ComponentModel;

namespace WaspbaneModels
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }

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

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

            SettingsControl.BaseURL = Context.Parameters["assemblypath"];
            SettingsControl.Save();
        }

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

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

In this code SettingsControl is the class that takes care of the Settings simply with some properties. I used this class so that my Windows Forms project also can access these settings.

In the Custom Actions tab I also added the Primary Output to everything. I am also not sure if this is correct because I think the Primary Output contains all .dll files including the Class Libraries.

I am not really sure where to continue from this point however because the Settings are not saved. I simply check this by putting up a messagebox at the startup of the application giving me the value of the setting.

Anyone interested in more data of the project just ask. I just won't post everything at once yet.

EDIT:

After some more testing I managed to find out that the Installer class isn't being called upon. I added something that would write out to a file when the method would've been called but nothing happened.

Therefore my question now is: How do I add the Custom Action to the Setup project properly?


回答1:


You can add the custom action class (.dll) to the Setup project. You have to add the InstallerActions.dll to the Install and Commit actions.

To add the custom action

  1. Select the Setup Installer project in Solution Explorer. On the View menu, point to Editor, and then click Custom Actions. The Custom Actions Editor is displayed.
  2. In the Custom Actions Editor, select the Commit node. On the Action menu, click Add Custom Action.
  3. In the Select Item in Project dialog box, double-click the Application Folder. Select Primary output from InstallerActions project. Primary output from InstallerActions appears under the Commit node in the Custom Actions Editor.
  4. In the Properties window, make sure that the InstallerClass property is set to True (this is the default).
  5. In the Custom Actions Editor, select the Install node and add Primary output from InstallerActions to this node as you did for the Commit node.



回答2:


The settings paradigm doesn't work with installer classes because you are not in a running application context. You're being called via reflection from an msiexec.exe process running with the system account without any working directory context. People typically use Xml processing directly on the settings file to get around this behavior. You need to name the full path to the settings file because, again, you are running as a callback from an msiexec.exe process.

The custom action doesn't need to be a Commit custom action because all VS custom actions run after all the files are installed. The only time you need a Commit custom is action is when you run code that has a dependency on a file being installed into the GAC on WinSxS because they are not accessible until Commit time.



来源:https://stackoverflow.com/questions/38357038/c-sharp-setup-project-custom-action-to-save-the-installation-directory-to-custom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!