桌面端的移动运算(二)

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 类时,第一步是要与设备建立一个连接。

** 注意: ** 在桌面程序中使用 RAPI 必须要有一个 PC 与设备之间的激活的 ActiveSync 连接。

在这篇文章包含的示例程序中,在 Form_Load 事件中将连接到设备。为了连接到设备,你使用 RAPI 类的 ** Connect ** 方法。正如下面代码所演示的,我紧接着检查了 RAPI 类的 ** DevicePresent ** 属性,来校验连接是否成功。

[VC#.NET]


try


        


//  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. **

OpenNETCF.Desktop.Communication 命名空间 RAPI 类提供了两个方法来拷贝文件—— ** CopyFileToDevice ** 和 ** CopyFileFromDevice ** 。这两个方法都将源文件作为第一个参数,而目的文件作为第二个参数。

BtnCopyPerform 按钮的点击事件处理过程里演示了这些方法。究竟是 ** CopyFileToDevice ** 或者 ** CopyFileFromDevice ** 方法被调用,要依赖于用户通过用户界面上的 Combo Box 进行选择。

[VC#.NET]


private void btnCopyPerform_Click(object sender, System.EventArgs e)


      {


      


// Perform the copy.


  try


  {


    if ((txtCopySource.Text == "") || (txtCopyDestination.Text == ""))


    {


      MessageBox.Show("You must provide both a source and destination   

file.",


        "Missing File Information");


    }


    else


    {


   switch (cmbCopyDirection.Text)


   {


        case "":


       MessageBox.Show("You must select a direction before initiating   

the copy.",


         "No Destination Selected");


        break;


     case "from desktop to device":


          myrapi.CopyFileToDevice(txtCopySource.Text,   

txtCopyDestination.Text);


       MessageBox.Show("Your file has been copied.", "Copy Success");


       break;


     case "from device to desktop":
Published At
Categories with Web编程
Tagged with
comments powered by Disqus