Working with the rapi class
To simplify the operation of the RAPI class, I will add a Using declaration to the C # version of the sample program, or add an imports to VB.NET, as follows:
[Vc # .NET]
Using opennetcf.desktop.communication;
[Vb.net]
Imports OpenNetcf.desktop.communication
Also, I will add a separate module-Level variable, MyRAPI, used to save an instance of a RAPI class.
[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
When your desktop program uses the RAPI class, the first step is to establish a connection with the device.
Note: Use the RAPI in the desktop program to have an activation ActiveSync connection between the PC and the device.
In the sample program included in this article, the device will be connected in the Form_Load event. To connect to the device, you use the RAPI class Connect method. As demonstrated by the following code, I immediately check the DevicePresent attribute of the RAPI class to verify that the connection is successful.
[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 exception
Messagebox.show ("The Following Error Occurred While Attempting To
Connect to "& _
"Your Device -" & ex.Message, "Connection Error")
Application.exit ()
END TRY
After the connection is determined, we will prepare to explore the features provided by RAPI. We will start from how to manage devices in your desktop. Working with files
RAPI provides a lot of functions in order to operate files in the folder. I will select the presentation of the three files: copy it from the device, copy the file, move the file on the device, and delete the file on the device. Let's take a look at the copy file.
Copying files to and from a device
One of the easiest ways with device exchange data is to simply copy a text or XML file between the device and the PC. The interface using the RAPI sample program is shown in Figure 1. Text and XML-based files can be used as a simple way to store application data or formulated data in mobile applications.
Figure 1. The copy file tab of the rapi demo program.
The OpenNetcf.desktop.communication Namespace RAPI class provides two ways to copy files - CopyFiletodevice and CopyFileFromDevice. Both methods use the source file as the first parameter, and the destination file is used as the second parameter.
These methods are demonstrated in the Click event handle of the BTNCopyPerform button. It is called the CopyFileFromDevice method to be called, depending on the user's choice over the user interface.
[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.copyFiletode (txtcopysource.text,
TXTCopyDestination.Text);
Messagebox.show ("Your File Has Been Copied.", "Copy Success");
Break;
Case "from device to desktop":