This article introduces how to freely control the installation process and how to integrate the automatic write registry function into VS program.

Source: Internet
Author: User
Tags visual studio installer

This article introduces how to freely control the installation process and how to integrate the automatic write registry function into VS program.

Recently, due to the need for more flexible control of program installation procedures and custom installation behaviors during project deployment, we specially studied VS program packaging and shared the solution with you.

Take VS2010 as an example:

This is a basic Visual Studio Installer solution interface that has been configured. The installation package generated by this interface is the most basic installation function. A lot of detailed tutorials on program packaging are provided online, so I will not repeat them here.

During project deployment, the user's registry needs to be modified at the same time, and the registry needs to be modified according to the installation path selected by the user. Therefore, the registry function provided by the VS package does not apply to me, because after the registry is imported, the installation path cannot be uploaded to the Registry.

 

 

So we needCustom CodeTo control the installation process through the installation program events.

Solution-> Add-> new project-> class library, named ExtensionLibrary. Under the project ExtensionLibrary, delete Class1.cs, add-> new project-> installer class (named InstallerDemo. cs), open InstallerDemo. cs, this class InstallerTest inherits from System. configuration. install. installer, which is a constructor. The functions that cannot be completed by the installer can be implemented by writing code in this class.

 

 

Example: add this in the constructor. afterInstall + = new InstallEventHandler (InstallerDemo_AfterInstall), implementation method InstallerDemo_AfterInstall (object sender, InstallEventArgs e), you can complete what the program will do after installation, the main events used by the Installer are BeforeInstall, AfterInstall, AfterRollback, AfterUninstall, BeforeRollback, and BeforeUninstall.

Now I need to add some values to the system registry based on the user's installation path after the installation is complete:

Public InstallerDemo () {InitializeComponent (); this. afterInstall + = new InstallEventHandler (InstallerDemo_AfterInstall);} private void InstallerDemo_AfterInstall (object sender, InstallEventArgs e) {string path = this. context. parameters ["targetdir"]; // obtain the installation target path set by the user. Note, add/targetdir = "[TARGETDIR] \" string [] commands = new string [4] To CustomActionData in the property bar of custom operations in the Setup project. commands [0] = "/c" + "reg add HKCR \ renke/f/ve/d \" URL: HyperText Transfer Protocol with Privacy \""; commands [1] = "/c" + "reg add HKCR \ renke/f/v \" URL Protocol \ "/d \"\""; commands [2] = "/c" + "reg add HKCR \ renke \ shell/f/ve/d \" open \""; commands [3] = "/c" + "reg add HKCR \ renke \ shell \ open \ command/f/ve/d \" \ "" + path + "\" + "Gyn5tDataAnalyze.exe \" \ "% 1 \\\"\""; foreach (var command in commands) {Process p = new Process {StartInfo = {FileName = "cmd.exe", Arguments = command, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, createNoWindow = true }}; p. start (); p. standardInput. writeLine ("exit"); p. close ();}}

 

It is not enough to complete the InstallerDemo_AfterInstall method of the above custom code, but it will not execute the code in InstallerDemo_BeforeInstall after installation. The "Custom operation Editor" is also used ". Project SetupDemo-> Add-> project output-> select ExtensionLibrary and click OK. You will find that the project SetupDemo contains an additional word "active output from ExtensionLibrary (activity.

 

 

OK. Now, go to "Custom operations". You can see four sub-items: installation, submission, rollback, and uninstallation. On the installation page, right click-> Add custom operation-> select "main output from ExtensionLibrary (activity )". In this way, the InstallerDemo_BeforeInstall code will be executed before the installation.

 

 

In addition, to obtain the user's installation directory, you must also note that, in the Custom operation-> Installation-> "main output from ExtensionLibrary (activity)", right-click the property, enter/targetdir = "[TARGETDIR] \" in CustomActionData, and use this statement in the InstallerDemo class. context. parameters ["targetdir"] to obtain the installation directory.

 

After installation, you can call the console to add a registry. You can also configure the registry based on the selected installation path.

 

With this method, you can flexibly handle the various things required by the installation package when VS Installer's built-in functions cannot meet the project requirements.

 

Later, we will introduce how to use the BS system to call the local CS program and pass parameters to the CS system.

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.