Thursday, April 30, 2009

Building an Installer for mobile .Net applications

In the process of developing a mobile application for the IT department at my school, I needed to build an installer for the mobile application to be installed from a desktop pc. To accomplish this I had to build three projects in addition to the main project of the application. To make life easier for fellow developers I will go through this process step by step.

Prerequisites:

As far as prerequisites the only requirement for this tutorial outside of Visual Studio 2008 is Windows Mobile Device Center (WMDC) which can be found here -> WMDC.

Your Application:

After you've completed your mobile application or you just want to do some testing; the first task that we have to complete is to create a .Cab file for our application and associated files. Cab files are basically compressed .exe files that can be used to install applications on mobile devices. The only drawback is the Cab file has to be on the device in order to install it. This could become very tedious if you have lots of Cab files and it's not something you want to make your users do. For more information on Cab Files see this -> Cab Files .

I will provide screen shots through out this tutorial but most of the information will be conveyed through text. So lets get started. Below is a screen shot of my application. It's very simple, all it does is display "Hello World" on the mobile device when ran.


So lets go ahead and setup the Cab file that will store your application and all the files assocaited with your application.

Your Applications Cab File:
  1. Add a new project to your existing project by going to File->Add->New Project
  2. Under project types, click other project types->Setup and Deployment then select Smart Device CAB Project. Name is something along the lines of "Your Project" Setup.
  3. After that select the CAB project that you just added to your application and open it's properties. There are a couple of things you are going to want to change. You want to change the manufacturere name to a name you choose and change the product name to a name of your choice.
  4. Now right click on the CAB project ->Add->Project Output. This is where we are going to tell Visual Studio what files from our project we need to include in the CAB file. There is an explanation of each output option when you select it. For simplicity where just going to add the Primary Output, Content Files and, Localized Resources.
  5. Now right click on the CAB project->View->File System. There is a folder called "Application Folder". By right clicking on this folder you can add any files that are associated with your application. You can also add subfolders. Basically the items in this folder will be the items that are in your applications folder on the device, so it would be wise to put start menu icons or any files your application needs etc. in this folder.
  6. I went ahead and added a 16 x16 pixel icon in the folder to demonstrate something later in the turorial. Feel free to add your icon is this folder.
  7. While viewing the file system of the CAB file, right click the parent folder of the "Application Folder" named "File System on Target Machine". Select Add Special Folder->Start Menu Folder.
  8. Click the Start Menu Folder and on the right side of the file system view Right click on the white space and select Create New Shortcut. Select Application folder to look for the icon you placed in it earlier and select it.
  9. Click the icon and open its properties change the name to the name of your application. Click on target and open the explorer->Look in your Application folder for your Primary Output and select it. This basically tills the operating system to open your application when the icon is clicked.
  10. The final step conserning your applications CAB file is to right click on the CAB File Project and select Build. Like the name suggest this builds your CAB file.
If you followed the steps correctly your application should look like the screen shot below. Note: You may have alot more files than I do since your application is something useful. Don't be alarmed!!!


Now comes the hard part! Just kidding but this section does involve alot more steps. So now we have to make a windows installer that will install all the files in your File Systems folders on the desktop pc. After those files are available locally, the windows installer will open a program called CEAppManager which is apart of the windows mobile device application (which was our prereq.) and install our mobile application from there.

Windows Installer 1 of 2:
  1. Add a new project by going to File->Add->New Project, Under project types, click other project types->Setup and Deployment then select Setup Project, name it something like "Your Applicaton Name" Windows Installer.
  2. Right click on the new project and select View->File System. Just as we did in the project we made earlier right click the application folder and select Add->File. Now you are going to want to add the cab file for your application and any other cab files that you need. For example when developing my application I also neeeded sql replication, sqlCE Cab files etc. Ok, Your Cab file should be located in your applications folder. This should be the direct path of your Cab file "Your Applications Folder"->"Your Cab Application Folder"->Debug->"Your Application Cab File". For my project my Cab File will be located in -> Tutorial1->Tutorial Installer->Debug->Tutorial Installer.Cab. Note:If you dont see a debug folder then you need to go back and Build the Cab Application.
  3. Once the Cab file is in your application folder you need to associate an ini file with the Cab file. You must do this for every Cab file that you add to the application folder. Format the ini file just like the one below. Just use notepad and save the file as an ini file an add the file to your application folder. Make sure you type version = 1.0 and CabFile is the name of the Cab that the ini file is for.



Windows Class Library:
  1. Add a new project by going to File->Add->New Project and add a Class Library Project and name it something like "Your Application Name" Installer Class. Delete the Class1.cs file that is apart of this project because we wont be using this file.
  2. Right click on your Installer Class project and add a new item by going to Add->New Item and add an Installer Class file. For simplicity name it InstallerClass.cs.
  3. Right click the InstallerClass.cs and click view code. The fun starts now!!!
  4. Add the following assemblies at the top of your code: using Microsoft.Win32, using System.Reflection, using System.IO, using System.Diagnostics, using System.Windows.Forms
  5. Copy the code below by looking at it and typing it or downloading the source code from the link at the end of this tutorial. I'll explain this code below the screen shots and the only thing you need to change is the iniFileName. All you have to do is change that to the name of your ini File name frome earlier in the tutorial.

    The CeAppRegPath variable stores the registry path of the CEAppManager so we wont have to depend on the file path of the programs folder that might not be consistant from system to sytem. iniFileName self explanatory, filePreFix just concatenates the file path with the prefix file:/ so we wont have broken file paths. The OnAfterInstall method basically gets the file name of the CEAppManager and the file path of the ini file. Then it opens the CEAppManager, gets the argumets (which are basically the Cab files that you provide it) and starts the CEAppManager to start the installation of your application. The ceAppMgrExe method gets the file path of the program through the registry and the IniFilePath method gets the file path of the ini file that you provided. There is one more step after this then your done.
Windows Installer 2 of 2:
  1. Right click on your windows installer which should be named "Your Application name" Windows Installer, if you've been following the tutorial. Click Add->Project Output. The very top drop down list specifies what project you want to get the output from. Select the Installer Class project which should be named "Your Application name" Installer Class and select Primary output.
  2. Right click the windows installer again and Click View->Custom Actions. There should be a folder called install. Right click that folder and select->Add Custom Action. From the drop down list choose the Application Folder to look in. You should see the primary output from installer class. Choose the primary output and select OK.
  3. Now Build the Windows Installer by Right Clicking it and selecting Build.

You can now access your installer which will be located in your Windows Installer folder. The file path should be similar to this one: "Your Application Name" Windows Installer folder->Debug->Your Windows Installer(.exe) file

Note:If you change anything in your main application, then you will have to update your Cab file that is in the Windows Installer. To do this all you have to do is Rebuild your Cab Application, then delete the Cab file thats in your Windows Installer application folder and add the rebuilt one or just overwrite the old Cab file with the new one. After that you just have to rebuild the Windows Installer and thats it. You now have an installer for your mobile application.

If there are any problems or questions feel free to leave a comment or send me an email through my home page at www.johnastevens.com.

Dowload Source Files: Tutorial Installer