Create a C # installer/customize installation events using the installation service class

Source: Internet
Author: User

Step 1: Create a project
1. Open vs, create a project, select another project type, and click Install and deploy (). Create an installation project named setuptest. 2. after creating a project, you can see the Sub-item in Solution Explorer: File System Editor, Registry Editor, file type editor, User Interface Editor, custom operation editor, and start the condition editor. The following is an example of how to use it in different places.
3. The simplest Installer: click "file type editor", find "application folder", add-> file, and select the program file you want to package (for example, my program consists of run.exe and Lib. dll. Click Generate on the project "setuptest" to complete a simple installation package. After the installation is generated, click Install. If there is no error, you can now see the effect of your installation package.
4. How to customize code? In the production process, these simple functions cannot meet your requirements, so write your own code to complete it.
Solution-> Add-> new project-> class library, name it library, delete class1.cs under the project library, add-> Create Project-> install program class (Name: installertest. CS), open installertest. CS, you will find that this class installertest inherits from the installer, and there is a constructor in it. Now, you can write code in this class to implement functions that cannot be completed by the installer. For example, add this. beforeinstall + = new installeventhandler (installertest_beforeinstall) to the constructor. Implementation Method
Installertest_beforeinstall (Object sender, installeventargs E), you can complete the things to be done before the program is installed. The main events used by the installer are similar to the following: beforeinstall, afterinstall, afterrollback, afteruninstall, beforerollback, beforeuninstall, which has obvious significance. You don't need to explain it.
 
5. The installertest_beforeinstall event in Step 4 has been completed. Is it necessary to execute
What about the code in installertest_beforeinstall? The answer is no. The "Custom operation Editor" is also used ". Project setuptest-> Add-> project output-> select library-> select main output and content file and then confirm. You will find that, under the project setuptest, an additional item "primary output comes from library (activity)" and a "content file comes from
Library (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 library (activity )". In this way, the installertest_beforeinstall code will be executed before the installation. (Of course, if you want to do something before and after the uninstallation, you must add custom operations in the "uninstallation" area. Otherwise, even if the event afteruninstall is monitored, it won't be executed either. The complete solution is to add the main output from the library (activity) in the installation, submission, rollback, and uninstallation processes)

6. How do I get the installation path selected by the user? When implementing the Code, the installation path selected by the user is used in most cases. How can we get the value of this path? Choose Custom operations> Installation> main output from library (activity), right-click the attribute, and enter/targetdir = "[targetdir] \" in customactiondata. then use this statement in the installertest class. context. parameters ["targetdir"] can obtain the installation directory.
 
7. How can I get more information input during installation? During the installation, you may also need a lot of other information. For example, a previously created installation file contains the installation database. During the installation, you must enter the Database Password, user name, and other information. The implementation is as follows: User Interface-> Installation-> Start-> right-click-> Add dialog box-> select "text box (a)", you can see that the text box (a) has many attributes, such: edit1label, edit1property, edit1value, edit1visible, etc. edit1label is the prompt of the content to be input in this input box. edit1value is the input content. For example, fill in the edit1label content with "Enter Database User name:", fill in edit1property content with dbuser, and select "true" for edit1visible. In addition, in the customactiondata mentioned in section 6, add/dbuser = "[dbuser]" and use this statement in installertest. context. parameters ["dbuser"] can be used to obtain the user name you entered during installation (other methods such as single quotes, check boxes, and button buttons get values almost all, ). in short, after adding some dialog boxes, you must add a/parm = "[attribute value]" in customactiondata to get the modified value in the code.

Note: If you want to add multiple parameters to customactiondata, separate them with spaces. If the parameter is a path, the format is/parm = "[attribute value] \".
8. Delete the installation file. The above are all done, and an installer is basically complete. Run the installation program and prompt that the installation is complete. If you open the installation directory, you will find many. tmp files. This is a temporary file used for installation and should be deleted at the end. Add an installertest_afterinstall event, and write the deletion code in the event. Idea: retrieve all the files in the installation directory, filter out the. tmp files, and delete them. My practice is as follows:
File. Delete (this. Context. Parameters ["targetdir"] + "test"); string [] filenames =
Directory. getfiles (this. Context. Parameters ["targetdir"]); int I = 0;
While (I <filenames. Length & filenames [I]! = NULL ){
If (filenames [I]. substring (filenames [I]. Length-3, 3) = "tmp "){
File. Delete (filenames [I]);} I ++;
}
Note: introduce the system. Io namespace.


 

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.