桌面端的移动运算(二)

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":
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值