桌面端的移動(dòng)運(yùn)算(二)
發(fā)表時(shí)間:2023-08-13 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]Working with the RAPI Class為了簡(jiǎn)化RAPI類的操作,我將添加一個(gè)using聲明到示例程序的C#版本,或添加一個(gè)Imports聲明到VB.NET中,示例如下:[VC#.NE...
Working with the RAPI Class
為了簡(jiǎn)化RAPI類的操作,我將添加一個(gè)using聲明到示例程序的C#版本,或添加一個(gè)Imports聲明到VB.NET中,示例如下:
[VC#.NET]
using OpenNETCF.Desktop.Communication;
[VB.NET]
Imports OpenNETCF.Desktop.Communication
另外,我將添加一個(gè)單獨(dú)的module-level變量,myrapi,用于保存一個(gè)RAPI類的實(shí)例。
[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
當(dāng)你的桌面程序使用RAPI類時(shí),第一步是要與設(shè)備建立一個(gè)連接。
注意:在桌面程序中使用RAPI必須要有一個(gè)PC與設(shè)備之間的激活的ActiveSync連接。
在這篇文章包含的示例程序中,在Form_Load事件中將連接到設(shè)備。為了連接到設(shè)備,你使用RAPI類的Connect方法。正如下面代碼所演示的,我緊接著檢查了RAPI類的DevicePresent屬性,來(lái)校驗(yàn)連接是否成功。
[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
在連接確定后,我們將準(zhǔn)備來(lái)探索RAPI提供的功能了。我們將從在你的桌面程序中如何管理設(shè)備的文件夾和文件開(kāi)始。
Working with Files
RAPI提供了很多的功能為了操作文件夾中的文件。我將選擇示范三個(gè)文件相關(guān)的屬性:從設(shè)備上拷入、拷出文件,在設(shè)備上移動(dòng)文件,在設(shè)備上刪除文件。我們先來(lái)看一下拷貝文件。
Copying Files To and From a Device
與設(shè)備交換數(shù)據(jù)最容易的方式之一就是簡(jiǎn)單地在設(shè)備與PC間拷貝一個(gè)文本或者XML文件。該操作的使用RAPI示例程序的界面如圖一。文本和基于XML的文件可以在移動(dòng)應(yīng)用程序中作為存儲(chǔ)應(yīng)用程序數(shù)據(jù)或配制數(shù)據(jù)的簡(jiǎn)單方式。
Figure 1. The Copy File tab of the RAPI demo program.
OpenNETCF.Desktop.Communication命名空間RAPI類提供了兩個(gè)方法來(lái)拷貝文件——CopyFileToDevice 和 CopyFileFromDevice。這兩個(gè)方法都將源文件作為第一個(gè)參數(shù),而目的文件作為第二個(gè)參數(shù)。
BtnCopyPerform按鈕的點(diǎn)擊事件處理過(guò)程里演示了這些方法。究竟是CopyFileToDevice 或者 CopyFileFromDevice方法被調(diào)用,要依賴于用戶通過(guò)用戶界面上的Combo Box進(jìn)行選擇。
[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":