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

### Moving Files on a Device&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;

也许你会遇到需要在设备上移动或者重命名一个文件的情况。例如，你也许想在拷贝一个新版本的文件到设备上之前先为配置文件做一个备份。该功能的演示界面如图2。

&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/27154/CSDN\_Dev\_Image\_2004-4-231732210.gif" o:href="http://msdn.microsoft.com/library/en-us/dnroad/html/road0324200402.gif"&gt;&lt;font face="宋体"&gt;&lt;/font&gt;&lt;/shape&gt;

**Figure 2. The Move File tab of the RAPI demo program**

OpenNETCF.Desktop.Communication命名空间RAPI类提供了**MoveDeviceFile**方法用于移动或重命名一个文件。作为CopyFile方法，这个方法将源文件作为第一个参数，将目的文件作为第二个参数。

BtnMovePerform按钮的点击事件过程演示了**MoveDeviceFile**方法。

```

[VC#.NET]
```

```

private void btnMovePerform_Click(object sender, System.EventArgs e)
```

```

{
```

```

// Perform the move.
```

```

 try
```

```

 {
```

```

 if ((txtMoveSource.Text == "") || (txtMoveDestination.Text == ""))
```

```

 {
```

```

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

```

"Missing File Information");
```

```

 }
```

```

 else
```

```

 {
```

```

 myrapi.MoveDeviceFile(txtMoveSource.Text, txtMoveDestination.Text);
```

```

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

```

 }
```

```

 }
```

```

```

```

// Handle any errors that might occur.
```

```

 catch (Exception ex)
```

```

 {
```

```

 MessageBox.Show("The following error occurred moving the file " + 
```

```

ex.Message,"Connection Error");
```

```

 }
```

```

```

```

}
```

```

[VB.NET]
```

```

Private Sub btnMovePerform_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnMovePerform.Click
```

```

```

```

' Perform the move.
```

```

 Try
```

```

 If (txtMoveSource.Text = "") Or (txtMoveDestination.Text = "") Then
```

```

 MessageBox.Show("You must provide both a source and destination
file.", _
```

```

 "Missing File Information")
```

```

 Exit Sub
```

```

 End If
```

```

```

```

 myrapi.MoveDeviceFile(txtMoveSource.Text, txtMoveDestination.Text)
```

```

```

```

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

```

```

```

' Handle any errors that might occur.
```

```

 Catch ex As Exception
```

```

 MessageBox.Show("The following error occurred moving the file -" 

& ex.Message, _
```

```

 "Move Error")
```

```

 End Try
```

```

```

```

End Sub
```

### Deleting Files from a Device

你会发现你会经常在使用RAPI拷贝文件的同时使用删除文件的方法。比如，你的桌面程序也许会从设备上拷贝一个应用程序用来存储数据的文件，然后在成功执行拷贝任务的基础上，正确转换并从设备上删除该文件，使移动应用程序准备开始重新刷新。该操作的界面如图3。

&lt;shape id="\_x0000\_i1026" style="WIDTH: 292.5pt; HEIGHT: 178.5pt" type="#\_x0000\_t75" alt=""&gt;&lt;img src="/Develop/ArticleImages/27/27154/CSDN\_Dev\_Image\_2004-4-231732212.gif" o:href="http://msdn.microsoft.com/library/en-us/dnroad/html/road0324200403.gif"&gt;&lt;font face="宋体"&gt;&lt;/font&gt;&lt;/shape&gt;

**Figure 3. The Delete File tab of the RAPI demo program**

OpenNETCF.Desktop.Communication命名空间RAPI类提供**DeleteDeviceFile**方法用来删除设备文件。你想删除的文件作为该方法的第一个参数。

BtnDeletePerform按钮的点击事件演示了**DeleteDeviceFile**方法。

```

[VC#.NET]
```

```

private void btnDeletePerform_Click(object sender, System.EventArgs e)
```

```

{
```

```

```

```

// Perform the delete.
```

```

 try
```

```

 {
```

```

 if (txtDeleteFile.Text == "")
```

```

 {
```

```

 MessageBox.Show("You must provide a file to delete.",
```

```

 "No File Provided");
```

```

 }
```

```

 else
```

```

 {
```

```

 myrapi.DeleteDeviceFile(txtDeleteFile.Text);
```

```

 MessageBox.Show("Your file has been deleted.", "Delete Success");
```

```

 }
```

```

 }
```

```

```

```

// Handle any errors that might occur.
```

```

 catch (Exception ex)
```

```

 {
```

```

 MessageBox.Show("The following error occurred while deleting the 

file -" + 
```

```

ex.Message, "Delete Error");
```

```

 } 
```

```

}
```

```

[VB.NET]
```

```

Private Sub btnDeletePerform_Click(ByVal sender As System.Object, ByVal 

e As System.EventArgs) Handles btnDeletePerform.Click
```

```

```

```

' Perform the delete.
```

```

 Try
```

```

 If (txtDeleteFile.Text = "") Then
```

```

 MessageBox.Show("You must provide a file to delete.", _
```

```

 "No File Provided")
```

```

 Exit Sub
```

```

 End If
```

```

```

```

 myrapi.DeleteDeviceFile(txtDeleteFile.Text)
```

```

```

```

 MessageBox.Show("Your file has been deleted.", "Delete Success")
```

```

```

```

' Handle any errors that might occur.
```

```

 Catch ex As Exception
```

```

 MessageBox.Show("The following error occurred while deleting the 

file -" & ex.Message, _
```

```

 "Delete Error")
```

```

 End Try
```

```

```

```

End Sub
```

现在我们来回顾一下文件相关的三个方法：拷贝，移动和删除。下面我们来看看如果从桌面程序中在设备上启动一个应用程序。