简体   繁体   中英

Visual Studio Installer with to set path of script file

I have a Visual Studio installer that installs a Windows Service hosting a Web Service.

What I am trying to resolve is to give users during installation a choice of where a particular script is located on the box and once they have set the location to then update the location to the app.config file which the application can then use.

At the moment during install I have added a custom folder and added to that the folder the file. This all gets installed. I want to keep this as a default, but to be able to overwrite it with the users new choice.

I have added a user interface and a text box to capture the user's choice, and a installer class, but this is failing when I try it. Where it is failing is when it is try to find my App.Config file. It says that it cannot find it. In my code, I have written:

    string path = Assembly.GetExecutingAssembly().Location;

So, to me my app.config has not been installed into this location when the installation is running at that moment in time.

So, can someone please advise how I can override and save the new location.

Thanks

What you're doing won't work for a number of reasons.

  1. Custom actions in VS setup projects run after all the files have been installed, so it's too late to choose a folder where files can be installed. UI is supposed to be at the front where the normal wizard UI forms run, but VS setups don't support custom dialogs.

  2. Installer classes with UI tend to do strange things because they are called from the execute sequence in the MSI, and the apartment threading won't work.

  3. You're not running in an interactive user environment. Your installer class is being called from an msiexec process running with the system context. If you need to load files, name the path explicitly.

If you're going to stick with Visual Studio setups, your best bet is to install the script to a normal default location and have your app offer a way to copy it somewhere. Then the user can copy it somewhere new whenever they want and you're running in a normal user environment.

You can not get the directory full path where the MSI is being installed using,

string path = Assembly.GetExecutingAssembly().Location;

above code will probably return a path C:\\Windows\\System32 which has the msiexec.exe location which handles the MSI installation. Use the following code within installer class to get the installation path.

string installationPath = Context.Parameters["assemblypath"];

Rest of your questions are not clear.

Folks

Managed to resolve this myself.

I added an afterinstall event to the Installer class. In this event I added this line of code

        string assembley = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(assembley);
        string path = Uri.UnescapeDataString(uri.Path);
        string directory = Path.GetDirectoryName(path);

To get the config file - I did this

       Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(ProjectInstaller)).Location);

This gives me what I want and it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM