---
title: "桌面端的移动运算(二)"
author: "mmdev"
date: "2004-04-26T10:55:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2004-04-26T10:55:00Z"
id: 990046
url: "https://mmdev.iteye.com/blog/990046"
---

## Working with the RAPI Class&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;

为了简化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的文件可以在移动应用程序中作为存储应用程序数据或配制数据的简单方式。

&lt;?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /&gt;&lt;shapetype id="\_x0000\_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"&gt;&lt;stroke joinstyle="miter"&gt;&lt;/stroke&gt;&lt;formulas&gt;&lt;f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/f&gt;&lt;f eqn="sum @0 1 0"&gt;&lt;/f&gt;&lt;f eqn="sum 0 0 @1"&gt;&lt;/f&gt;&lt;f eqn="prod @2 1 2"&gt;&lt;/f&gt;&lt;f eqn="prod @3 21600 pixelWidth"&gt;&lt;/f&gt;&lt;f eqn="prod @3 21600 pixelHeight"&gt;&lt;/f&gt;&lt;f eqn="sum @0 0 1"&gt;&lt;/f&gt;&lt;f eqn="prod @6 1 2"&gt;&lt;/f&gt;&lt;f eqn="prod @7 21600 pixelWidth"&gt;&lt;/f&gt;&lt;f eqn="sum @8 21600 0"&gt;&lt;/f&gt;&lt;f eqn="prod @7 21600 pixelHeight"&gt;&lt;/f&gt;&lt;f eqn="sum @10 21600 0"&gt;&lt;/f&gt;&lt;/formulas&gt;&lt;path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"&gt;&lt;/path&gt;&lt;lock v:ext="edit" aspectratio="t"&gt;&lt;/lock&gt;&lt;/shapetype&gt;&lt;shape id="\_x0000\_i1025" style="WIDTH: 292.5pt; HEIGHT: 178.5pt" type="#\_x0000\_t75" alt=""&gt;&lt;img src="/Develop/ArticleImages/27/27153/CSDN\_Dev\_Image\_2004-4-231729200.gif" o:href="http://msdn.microsoft.com/library/en-us/dnroad/html/road0324200401.gif"&gt;&lt;font face="宋体"&gt;&lt;/font&gt;&lt;/shape&gt;

**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":
```

```

```