Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Deployment  »  Installer Class and Custom Actions  »  Page 3
Installer Class and Custom Actions
by Arnaldo Sandoval | Published  10/15/2007 | Deployment | Rating:
Page 3
USING THESE EVENTS
  • You basically override the event you want to use by typing public override and space, then intellisense kicks in. 

     
  • Once you enter the event, its code should look like this:
Code:

public override void Commit(System.Collections.IDictionary savedState) 

   base.Commit(savedState);
}


  • The declaration for the Install event is a little bit different, as shown:

    Code:

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



    Its IDictionary parameter is named differently to the names used by the other events, here it is named stateSaver; It is very important to declare and use this event if you are saving installation parameters to be shared across all the installation events. For example, suppose that you want to pass the installation's target directory stored in the [TARGETDIR] parameter. You can do that with the code below in the Install event:

Code:


stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());


The complete Install event code including the line of code above should look like this:

Code:

public override void Install(System.Collections.IDictionary stateSaver) 
{ 
   base.Install(stateSaver); 
   stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString()); 
}


Now, let's explain this statement: the stateSaver object-variable (IDictionary type) is the parameter received by the Install event, while the Context.Parameters["DP_TargetDir"] come from the CustomActionData property defined at the Deployment Project's Install custom action, as shown below:



We named it, DP_TargetDir prefixed with the forward slash (/), We get its value from the deployment project's propery [TARGETDIR], notice that we are enclosing it between double quotes, and we added a backward slash (\) before closing the double quote; You must always add the backward slash before the final double quote for this parameter, otherwise you will get an unexpected behaviour at installation time.

The deployment project at installation time will add the CustomActionData parameters to the custom action's Context Parameters using the names chosen by you (DP_TargetDir in this example).

Whe you add an entry to the stateSaver dictionary, the installation process will create a file at the installation target directory with the base name of your Installer Class and the type InstallState, based on the samples names we are using in this explanation, the InstallState name will be:

Code:

MyInstallerClassDll.InstallState


Now, the savedState parameter passed to any event of your Installation Class will have the TargetDir as one of its entries.

  • If we want to prove the explanation given in the previous section regarding the usage of the stateSaver and savedState dictionary parameters, we may use the Commit event to do so, the code below will dump all the key-values found in its savedState parameters as well as the class' Context Parameter dictionary.

    Code:

    public override void Commit(System.Collections.IDictionary savedState) 

       base.Commit(savedState); 

       StreamWriter sw = new StreamWriter("C:\\Temp\\VbCity_caic_Commit.txt", false); 

       sw.WriteLine("savedState count : " + savedState.Count.ToString()); 
       sw.WriteLine("savedState keys : " + savedState.Keys.Count.ToString()); 
       sw.WriteLine("savedState values : " + savedState.Values.Count.ToString()); 

       foreach (string k in savedState.Keys) 
       { 
          sw.WriteLine("savedState key[" + k + "]= " + savedState[k].ToString()); 
       } 

       writeContext(sw); 

       sw.Flush(); 
       sw.Close(); 

    }



    A "using System.IO;" line of code was added at the top of the class for the code in the Commit event to work because it is using the StreamWriter.

    The commit event is writing to a file on the Temp folder all the values in the savedState dictionary object as well as the Context.Parameters dictionary, the lattest by the writeContext() method (its code shown below)

    Code:

    private void writeContext(StreamWriter wrkSW) 
    { 
       wrkSW.WriteLine("Context Parameters"); 
       wrkSW.WriteLine("Count : " + Context.Parameters.Count.ToString()); 
       wrkSW.WriteLine("Keys : " + Context.Parameters.Keys.Count.ToString()); 
       wrkSW.WriteLine("Values : " + Context.Parameters.Values.Count.ToString()); 

       foreach (string k in Context.Parameters.Keys) 
       { 
          wrkSW.WriteLine("ContextKey [" + k + "]=" + Context.Parameters[k].ToString()); 
       } 
    }



    When we run the deployment project with the Commit event code shown above, the text file (VbCity_caic_Commit.txt) contains the following data

 

Comments    Submit Comment

Comment #1  (Posted by jipfromparis@hotmail.fr on 10/23/2007)
Rating
Very instructive. Just a point, its always a good idea to take globalization into account. Network service account name is OS language dependent. Thus, on page 4, the first parameter for FileSystemAccessRule constructor should be :

new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null).Translate(typeof(NTAccount)).Value

The above line requires using the System.Security.Principal namespace. This said, thanks again for this article.
 
Comment #2  (Posted by an unknown user on 11/20/2007)
Rating
Great! Short but detailed. Thanks
 
Comment #3  (Posted by an unknown user on 11/21/2007)
Rating
Good One.
 
Comment #4  (Posted by Gil Yoder on 11/29/2007)
Rating
Just what I've been looking for! You won't believe how hard it was to find a good explanation of customer actions. Thanks!
 
Comment #5  (Posted by Iván Gracia on 12/14/2007)
Rating
Hi! Great article! Came across it while searching for a way to include a custom action that asks the user whther he wants to empty the install folder or not on uninstall. Is possible to do this with Orca? How can I add a custom action to an installer project, but only the uninstall part?

Thanks in advance!
 
Comment #6  (Posted by Arnaldo Sandoval on 12/14/2007)
Rating
Hi Ivan, it seems the answer to your question is on page 3, you can assign the custom action to the UnInstall action; if you want to talk about it, why don't you join VbCity (www.vbcity.com) and post a question there. I will be glad to help; my user name there is rock.

Cheers,
 
Comment #7  (Posted by Jimmy Huang on 12/25/2007)
Rating
Very useful
 
Comment #8  (Posted by an unknown user on 12/28/2007)
Rating
It was a brilliant explanation. Before this article I didn't understand how it worked, and I couldn't find a good explanation anywhere else.
 
Comment #9  (Posted by an unknown user on 01/15/2008)
Rating
Trapping the mouse within the form seems like a hack which will not work in all cases. Any other ideas?
 
Comment #10  (Posted by Arnaldo Sandoval on 01/15/2008)
Rating
I agree with you regarding the issue with the mouse, and the way it is handled; I had two options at the time I wrote the article (a) make the community aware of the problem, giving a possible workaround or (b) exclude any reference to the odd behaviour, which will frustrate those following these directions; I think that option (a) better services the community, if anyone knows a better way to resolve the mouse issue, please post the code or email it to me, I will gratefully amend the article with a recognition note to the person assisting me with the issues.
 
Comment #11  (Posted by vhjraerhlj on 01/23/2008)
Rating
Hello! Good Site! Thanks you! wmsoloxrdxhw
 
Comment #12  (Posted by Andrei on 01/28/2008)
Rating
I can use following parameters:

[TARGETDIR]
[TARGETVDIR]
[TARGETPORT]

But I need to pass Product Name. It this possible? If so, which parameter I should use?
Thank you.

 
Comment #13  (Posted by willfread on 01/29/2008)
Rating
great Article ...The way he/she present the Topic
 
Comment #14  (Posted by an unknown user on 01/30/2008)
Rating
Just what I was looking for! Step-by-step, very complete and very easy to follow. Great article!
 
Comment #15  (Posted by Tomek on 02/07/2008)
Rating
great article

really great
 
Comment #16  (Posted by Tom on 02/08/2008)
Rating
This is the most helpful thing I've found on custom actions. Thanks.
 
Comment #17  (Posted by an unknown user on 02/10/2008)
Rating
It gives through knowledge of installer class and it's uses
 
Comment #18  (Posted by Blumen on 02/17/2008)
Rating
Great article! Very useful indeed!!
 
Comment #19  (Posted by Mustafa on 02/22/2008)
Rating
Thanks for the great post.. That covers many points and details of installation and uninstallation, thanks again..
 
Comment #20  (Posted by Greg Askew on 03/02/2008)
Rating
Thank you for writing this article, it is very helpful!
 
Comment #21  (Posted by an unknown user on 03/15/2008)
Rating
Excellent and many thanks. This article really helped. A section on how to add shortcuts into the start/programs menu along with the application's icon (non default) would be a great addition.
 
Comment #22  (Posted by an unknown user on 03/17/2008)
Rating
I was able to create a custom install package with limited experience.
 
Comment #23  (Posted by an unknown user on 04/18/2008)
Rating
bcas it has the more detailed and helpful info than the other articles on the net have..
 
Comment #24  (Posted by ducnm on 04/25/2008)
Rating
Great article, but I have a problem. When I unistall program, i want it not delete some file in directory. How do you do?

 
Comment #25  (Posted by Mayank Kukadia on 04/29/2008)
Rating
Greate article. Greate Efort. Thanks a lot.
 
Comment #26  (Posted by Geof on 05/06/2008)
Rating


hi,
first, sorry for my english…
I have made an Installer Class, it works, but my trouble is when the user want to stop the installation, it doesn’t stop… I have a message, but the installation goes on.
I have made method override for “RollBack” , but the programm doesn’t get in on clicking Cancel_Button.
How can I made for stopping or calling the rollback method, when the Cancel_Button is clicking?
thanks

 
Comment #27  (Posted by Arnaldo on 05/07/2008)
Rating
Hi Geof,

Why don't you join www.vbcity.com and post your question there, it will be easier to assist you this way, it will not be practical to engage into a technical discussion inside these comments, once your issue is resolve we can post a link to the VbCity's thread.

Regards,
Arnaldo
 
Comment #28  (Posted by an unknown user on 05/07/2008)
Rating
Fantastic effort thank you for that. That helped me a lot.
 
Sponsored Links