Executing Custom Code During Installation
page 1 of 1
Published: 24 May 2004
Unedited - Community Contributed
Abstract
An application can consist of not only traditional program files, message queues, event logs, and performance counters on the deployment destination but also custom code that must be executed on the deployment destination. You can configure your application to execute the custom code when your application is installed.
by Vishal Kumar Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 10937/ 31

[Download Sample Code]

This article demonstrates how to perform a "custom action" on the target computer at the end of an installation.  Here we illustrate a sample to perform the custom action of copying the "Article.txt" file from the deployment destination directory, that is, from the ArticleSetup  directory--where the Setup files will be installed on the target machine, to the Office 2003  XLSTART directory (i.e., Program Files/Microsoft Office/OFFICE11/XLSTART).

 

 

Here's the deployment destination directory structure:

 

 

Here's the Office 2003  XLSTART directory structure, which is where the Article.txt file will be copied from the deployment destination directory:

 

 

 

The following steps will implement the above concept.

 

  1. Add a Class Library project.
  2. To this project, add an Installer class.  For more details, refer to the link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemconfigurationinstallinstallerclasstopic.asp.
  3. Write the following code in the Install method of the Installer class.
1:     public override void Install(IDictionary savedState)

2: {
3: string[] stradirs; //Used to store the Directories
4: //Copies the file from Deployment directory to the Xlstart
//directory

5: foreach (String str in Directory.GetLogicalDrives())
6: {
7: try
8: {
9: // Gets the XLStart Directory
10: stradirs = Directory.GetDirectories(@str,
"Program Files/Microsoft Office/OFFICE11/XLSTART");
11: //Directory Exists
12: if (stradirs.Length > 0)
13: {
14: Assembly objAssembly =
Assembly.GetAssembly(this.GetType());
15: string strPath = objAssembly.CodeBase;
16: // Replace the Primary Output Project Dll with
17: // spaces and get the path of the Deployment
18: // Directory
19: strPath = strPath.Replace("CustomDeployment.dll","");
20: strPath = strPath.Replace("//","");
21: strPath = strPath.Replace("file:/","");
22: // Gets the Deployment
23: // directory path
24: string strSetUpPath = strPath + "Article.txt";
25: // Gets the XLStart directory path
26: string strXLStartPath = stradirs[0] + "/Article.txt";
27: //Ensure that File Article.txt Exists
in Deploymnet Folder

28: if (File.Exists(strSetUpPath))
29: {
30: // Move the file.
31: if (File.Exists(strXLStartPath))
32: {
33: File.Delete(strXLStartPath);
34: File.Move(strSetUpPath, strXLStartPath);
35: }
36: else
37: {
38: File.Move(strSetUpPath, strXLStartPath);
39: }
40: }
41: break;
42: }
43: }
44: catch
45: {
46: }
47: }
48: }

  1. Add the SetUp project.
  2. In the File System Editor (SetUp), add the Article.txt file and set the class library project as the Primary Output project in the Application Folder.

       

  

 

  1. In the Custom Action Editor (Set Up), add the Primary Output project to the Install folder.
  2. Compile the Solution.

       

 

 

  1. Install the Deployment Project.
  2. Go to C:\Program Files\Microsoft Office\OFFICE11\XLSTART and find the Article.txt file.

Conclusion

Here we have demonstrated the process of executing custom code when the application is installed. This helps to implement the custom action during the Setup installation.

 

Software Requirements

  1. .NET Framework 1.0 or 1.1
  2. Office 2003

 

 



User Comments

Title: junior programmer   
Name: Bogdan
Date: 2006-05-04 5:09:54 AM
Comment:
Nice article, very well structured, the screenshots are very useful, what could be improved is some explanations about [RunInstaller(true)] and the way Visual Studio 2005 compiles the whole solution is also a bit strange. But still, a very useful article. Thanks!
Title: Too Good   
Name: Girish
Date: 2005-04-05 8:03:26 AM
Comment:
IT is very good and useful code.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-11-22 1:11:31 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search