Figure 18-36
Add two references to the project: System.Configuration.Install
and System.Windows.Forms
(see Figure 18-37).
Figure 18-37
Switch to the code view of the RSSReaderInstaller.cs
file and import the following namespaces:
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
Within the RSSReaderInstaller
class, define the INI_FILE
constant. This constant holds the name of the .ini
file that will be used by ActiveSync for installing the CAB file onto the target device.
namespace RSSReaderInstaller {
[RunInstaller(true)]
public partial class RSSReaderInstaller : Installer {
const string INI_FILE = @"setup.ini";
In the constructor of the RSSReaderInstaller
class, wire the AfterInstall
and Uninstall
events to their corresponding event handlers:
public RSSReaderInstaller() {
InitializeComponent();
this.AfterInstall += new
InstallEventHandler(RSSReaderInstaller_AfterInstall);
this.AfterUninstall += new
InstallEventHandler(RSSReaderInstaller_AfterUninstall);
}
void RSSReaderInstaller_AfterInstall(object sender, InstallEventArgs e) {
}
void RSSReaderInstaller_AfterUninstall(object sender, InstallEventArgs e) {
}
The AfterInstall
event is fired when the application (CAB file) has been installed onto the user's computer. Similarly, the AfterUninstall
event fires when the application has been uninstalled from the user's computer.
When the application is installed on the user's computer, you use Windows CE Application Manager ( CEAPPMGR.EXE
) to install the application onto the user's device.
The Windows CE Application Manager is installed automatically when you install ActiveSync on your computer.
To locate the Windows CE Application Manager, define the following function named GetWindowsCeApplicationManager()
:
private string GetWindowsCeApplicationManager() {
//---check if the Windows CE Application Manager is installed---
string ceAppPath = KeyExists();
if (ceAppPath == String.Empty) {
MessageBox.Show("Windows CE App Manager not installed",
"Setup", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return String.Empty;
} else return ceAppPath;
}
This function locates the Windows CE Application Manager by checking the registry of the computer using the KeyExists()
function, which is defined as follows:
private string KeyExists() {
//---get the path to the Windows CE App Manager from the registry---
RegistryKey key =
Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CEAPPMGR.EXE");
if (key == null) return String.Empty;
else
return key.GetValue(String.Empty, String.Empty).ToString();
}
The location of the Windows CE Application Manager can be obtained via the registry key: " SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CEAPPMGR.EXE
", so querying the value of this key provides the location of this application.
The next function to define is GetIniPath()
, which returns the location of the .ini
file that is needed by the Windows CE Application Manager:
private string GetIniPath() {
//---get the path of the .ini file---
return "\"" +
Path.Combine(Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location),
INI_FILE) + "\"";
}
By default, the .ini
file is saved in the same location as the application (you will learn how to accomplish this in the next section). The GetIniPath()
function uses reflection to find the location of the custom installer, and then return the path of the .ini
file as a string, enclosed by a pair of double quotation marks (the Windows CE Application requires the path of the .ini file to be enclosed by a pair of double quotation marks).
Finally, you can now code the AfterInstall
event handler, like this:
void RSSReaderInstaller_AfterInstall(object sender, InstallEventArgs e) {
//---to be executed when the application is installed---
string ceAppPath = GetWindowsCeApplicationManager();
if (ceAppPath == String.Empty) return;
Process.Start(ceAppPath, GetIniPath());
}
Here, you get the location of the Windows CE Application Manager and then use the Process.Start()
method to invoke the Windows CE Application Manager, passing it the path of the .ini
file.
Likewise, when the application has been uninstalled, you simply invoke the Windows CE Application Manager and let the user choose the application to remove from the device. This is done in the AfterUninstall
event handler:
void RSSReaderInstaller_AfterUninstall(object sender, InstallEventArgs e) {
//---to be executed when the application is uninstalled---
string ceAppPath = GetWindowsCeApplicationManager();
if (ceAppPath == String.Empty) return;
Process.Start(ceAppPath, String.Empty);
}
The last step in this section is to add the setup.ini file that the Windows CE Application Manager needs to install the application onto the device. Add a text file to the project and name it setup.ini. Populate the file with the following:
[CEAppManager]
Version = 1.0
Component = RSSReader
[RSSReader]
Description = RSSReader Application
Uninstall = RSSReader
CabFiles = RSSReader.cab
For more information about the various components in an .ini
file, refer to the documentation at http://msdn.microsoft.com/en-us/library/ms889558.aspx.
To build the project, right-click on RSSReaderInstaller in Solution Explorer and select Build.
Set the SmartDeviceCab1
project's properties as shown in the following table.
Property |
Value |
Manufacturer |
Developer Learning Solutions |
ProductName |
RSS Reader v1.0 |
Creating a MSI File
You can now create the MSI installer to install the application onto the user's computer and then invoke the custom installer built in the previous section to instruct the Windows CE Application Manager to install the application onto the device.
Using the same solution, add a new Setup Project (see Figure 18-38). Name the project RSSReaderSetup
.
Figure 18-38
Читать дальше