VS2005为Windows移动程序创建安装文件(2)

http://www.sina.com.cn 2006年05月10日 16:43  天极yesky

  在当前窗口里,右键点击File System on Target Machine,选择Add Special Folder->Start Menu Folder。

VS2005为Windows移动程序创建安装文件(2)
图5

  在当前窗口中SampleAppv1的快捷方式应该是可见的了。拖动它到Start Menu Folder。这个目录看起来应该如图6所示。这一步骤完成的工作是在程序安装时在用户的PocketPC的开始菜单中创建一个快捷方式。

VS2005为Windows移动程序创建安装文件(2)
图6

  下一步是创建CAB文件。进入到Build->Build SmartDeviceCab1来创建CAB文件工程。CAB文件其实是一个安装程序,但是只有对计算机技术有很多了解的人才知道怎么作。下一步的工作是创建一个和普通的PC上安装程序类似的基于窗口样式的安装程序。

  创建自定制的安装程序

  上一步中我们创建了CAB文件。其实从技术上来说这已经解决了安装问题。但是用户仍然需要知道如何将CAB文件传输到Pocket PC上,然后使用触笔来点击CAB文件启动安装过程。一个更好的方法是创建一个Windows安装程序来自动化安装过程。这样做的话,你需要写一些代码来启动Windows CE App Manager。App Manager是ActiveSync(用来在Pocket PC和计算机之间进行同步的程序,工作在Windows移动设备上,如Pocket PC和Smartphone)的一个部分,在Pocket PC上完成程序安装的任务。

  开始,在当前的solution中添加一个新的工程(File->Add Project)…点击Windows project type选择Class Library模板。给工程命名为CustomInstaller并且点击确定。

  你需要给你的工程添加两个引用。在Solution Explore中右键点击CustomInstaller,并且选择Add Reference…需要添加的是System.Configuration.Install和System.Windows.Forms。System.Configuration.Install命名空间提供了允许你为自己的组件编写自定制的安装文件的类,而System.Windows.Forms命名空间则提供了允许你通过Windows UI来和用户进行交互(如Messagebox等)的相关类。

  在Solution Explorer中双击Class1.vb,在文件中填入下列代码--SetupApp类:

Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Reflection
Imports System.IO
Imports Microsoft.Win32

<System.ComponentModel.RunInstaller(True)> _
Public Class SetupApp
Inherits System.Configuration.Install.Installer
Private Const INI_FILE As String = "\setup.ini"

Private Sub Installer_AfterInstall(ByVal sender As Object, _
ByVal e As System.Configuration.Install.InstallEventArgs) _
Handles MyBase.AfterInstall
 '---to be executed when the application is installed---
 Dim ceAppPath As String = GetWindowsCeApplicationManager()
 If ceAppPath = String.Empty Then
  Return
 End If
 Dim iniPath As String = GetIniPath()
 Process.Start(ceAppPath, iniPath)

End Sub

Private Sub Installer_AfterUninstall(ByVal sender As Object, _
ByVal e As System.Configuration.Install.InstallEventArgs) _
Handles MyBase.AfterUninstall
 '---to be executed when the application is uninstalled---
 Dim ceAppPath As String = GetWindowsCeApplicationManager()
 If ceAppPath = String.Empty Then
  Return
 End If
 Dim iniPath As String = GetIniPath()
 Process.Start(ceAppPath, String.Empty)
End Sub

Public Shared Function GetWindowsCeApplicationManager() As String
'---check if the Windows CE Application Manager is installed---
 Dim ceAppPath As String = KeyExists()
 If ceAppPath = String.Empty Then
  MessageBox.Show("Windows CE App Manager not installed", _
"Setup", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
  Return String.Empty
 Else
  Return ceAppPath
 End If
End Function

Public Shared Function GetIniPath() As String
'---get the path of the .ini file---
Return """" & _
Path.Combine(Path.GetDirectoryName( _
System.Reflection.Assembly. _
GetExecutingAssembly().Location), "Setup.ini") & """"
End Function

Private Shared Function KeyExists() As String
 '---get the path to the Windows CE App Manager from the registry---
 Dim key As RegistryKey = _
Registry.LocalMachine.OpenSubKey( _
"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CEAPPMGR.EXE")
 If key Is Nothing Then
  Return String.Empty
 Else
  Return key.GetValue(String.Empty, String.Empty)
 End If
End Function
End Class

  这个类里包含了调用Windows CE App Manager应用程序的代码,从而使得你的应用程序可以通过ActiveSync安装到用户的Pocket PC上。

  SetupApp类包含了2个事件和3个方法:

  ·Installer_AfterInstall事件:在安装过程启动后事件触发
  ·Installer_AfterUninstall事件:在反安装过程启动后事件触发
  ·GetWindowsCeApplicationManager:返回包含Windows CE App Manager的路径
  ·GetIniPath:返回Setup.ini文件的路径
  ·KeyExists:通过注册表检查当前机器上是否有Windows CE App Manager可用。

  SetupApp继承了Installer类,并且将RunInstallerAttribute属性设置为true(<System.ComponentModel.RunInstaller(True)>)。因此,Visual Studio的自定制安装程序或InstallUtil.exe将在程序安装时被触发。在安装时,SetupApp类首先检查是否当前系统里有Windows CE App Manager。如果有的话,程序将会通过Windows CE App Manager安装。(注:如果需要详细了解如何在.NET中创建自定制的安装程序,请查阅Building Custom Installer Classes in .NET)。

  当添加了这个类之后,就可以创建自定制的安装程序工程了。右键点击Solution Explorer中的CustomInstaller,并且选择Build。

发表评论 _COUNT_条
Powered By Google
不支持Flash
·城市对话改革30年 ·新浪城市同心联动 ·诚招合作伙伴 ·企业邮箱畅通无阻
不支持Flash