Working with the RAPI Class 为了简化RAPI类的操作,我将添加一个using声明到示例程序的C#版本,或添加一个Imports声明到VB.NET中,示例如下:
[VC#.NET] using OpenNETCF.Desktop.Communication; [VB.NET] Imports OpenNETCF.Desktop.Communication 另外,我将添加一个单独的module-level变量,myrapi,用于保存一个RAPI类的实例。
[VC#.NET] // Declare an instance of the RAPI object. RAPI myrapi; [VB.NET] ' Declare an instance of the RAPI object. Dim WithEvents myrapi As New rapi Connecting to the Device 当你的桌面程序使用RAPI类时,第一步是要与设备建立一个连接。
// Connect to the device. { myrapi.Connect(); while (! myrapi.DevicePresent) { MessageBox.Show("Please connect your device to your PC using ActiveSync and before clicking the OK button.", "No Device Present"); myrapi.Connect(); } }
catch (Exception ex) { MessageBox.Show("The following error occurred while attempting to connect to"+ " your device - " + ex.Message, "Connection Error"); Application.Exit(); } [VB.NET] Try ' Connect to the device. myrapi.Connect() Do While Not myrapi.DevicePresent MessageBox.Show("Please connect your device to your PC using ActiveSync and " & _ "before clicking the OK button.", "No Device Present") myrapi.Connect() Loop
Catch ex As Exception MessageBox.Show("The following error occurred while attempting to connect to" & _ " your device - " & ex.Message, "Connection Error") Application.Exit() End Try 在连接确定后,我们将准备来探索RAPI提供的功能了。我们将从在你的桌面程序中如何管理设备的文件夹和文件开始。
Working with Files RAPI提供了很多的功能为了操作文件夹中的文件。我将选择示范三个文件相关的属性:从设备上拷入、拷出文件,在设备上移动文件,在设备上删除文件。我们先来看一下拷贝文件。
Copying Files To and From a Device 与设备交换数据最容易的方式之一就是简单地在设备与PC间拷贝一个文本或者XML文件。该操作的使用RAPI示例程序的界面如图一。文本和基于XML的文件可以在移动应用程序中作为存储应用程序数据或配制数据的简单方式。
Figure 1. The Copy File tab of the RAPI demo program.