Applicability:
Microsoft. NET Compact framework
Windows Mobile-based Pocket PC
Windows Mobile-based smart phones
Microsoft ActiveSync
Abstract:Learn how to store data using remote APIs or rapis, and how to merge data into applications developed through. NET Compact framework.
Content on this page
Introduction
Remote API Library
Classes provided through the communication namespace
Use the communication library of opennetcf
Rapi
Use rapi
Connect to the device
Use Files
Start the application
Retrieve System Information
. NET Compact framework quick development
On the road
Introduction
In the previous article, I proposed the location involved in mobile computing. The focus is on data storage methods, especially the pocket Access database, and how to merge these databases into applications developed through. NET Compact framework.
This article continues to discuss the data storage method, but the angle is different. Specifically, it is mobile computing desktop. This aspect is often ignored by new mobile solution developers. Many developers work tirelessly on the mobile issues of applications, but the results ignore that desktop or server applications can become part of mobile solutions. Obviously, most mobile solutions are device-based, and almost all solutions finally need to pass in or out data from the settings. The data includes device-collected data, server-provided data for reference, configuration data, and even application update data. This article describes how to use a simple desktop application to make full use of the Remote API to perform the following operations:
Copy and paste files by device.
File in the mobile device.
Delete files from a device.
Start the application on the device.
Retrieves system information from a device.
The examples mentioned throughout this article are displayed in the form of Microsoft Visual C #. NET and Visual Basic. Net code. They are part of the "Two Sides of the road" column.
Back to Top Remote API Library
This month's application example demonstrates the functions of remote APIs or rapis. This API is a key component of ActiveSync. The rapi library consists of a group of functions that can be used to manage devices, including directory files, registry and system information of devices, through desktop applications.
To simplify the use of remote APIs, you can use the communication library (an Open Source Resource) organized by opennetcf to obtain opennetcf website from the following address ). This library removes all the headaches in rapi processing and provides a set of methods to demonstrate all the functions that rapi must provide.
Back to header: class provided through the communication namespace
The opennetcf. desktop. Communication namespace provides the following classes, as shown in figureTable 1.
Class |
Description |
ActiveSync |
Provides access to the underlying features of Microsoft ActiveSync. |
Autostartapps |
Provides the function and related information for configuring the application that runs at the beginning of an ActiveSync session. |
Fileinformation |
Describes the structure of the file information returned by the findfirstfile and findnextfile methods. |
Filelist |
A set of fileinformation classes. Desktop applications often use it to query and manage the file structures of connected devices. |
Rapi |
Provides simple packaging of basic rapi functions. |
Rapiexception |
Define exceptions for rapi classes. |
Registry |
Provides access to the basic registrykey class. These classes are used in turn to access and modify the registry value and registry subkeys of the connected device. |
Registrykey |
Defines an entry node in the device registry. |
As shown in the table above, the opennetcf. desktop. Communication namespace provides a large number of desktop functions, which cannot be described in detail in an article. This article focuses on the rapi class, especially the three main functions of the rapi class:
Manage the directory files in the device.
Start the application on the device.
Retrieves system information from a device.
These three items are selected because these three items play a particularly helpful role in developing mobile solutions.
Back to Top: Communication libraries using opennetcf
The opennetcf communication library is provided through the opennetcf. desktop. Communication. dll file. You need to include the reference of this library in the desktop application. To add a reference, follow these steps:
In Solution Explorer, right-click the references folder. The pop-up menu is displayed.
In the menu, select "add reference ".
The "add reference" dialog box is displayed. Select the "opennetcf. desktop. Communication" component.
Click OK to add the selected components to your project.
Back to Top rapi class
Table 2The listed rapi methods are most helpful for developing mobile applications.
Method |
Description |
Connect |
Establishes a synchronous connection with the device. |
Copyfilefromdevice |
Copy files from the device to the PC. |
Copyfileondevice |
Copy files from one location to another. |
Copyfiletodevice |
Copy files from the PC to the device. |
Createdevicedirectory |
Create a directory in the device. |
CreateProcess |
Start the application on the device. |
Deletedevicefile |
Delete files from a device. |
Devicefileexists |
Check whether files exist on the device. |
Disconnect |
Disconnect from the device. |
Enumfiles |
Provides a list of fileinformation class arrays that match the conditions provided by the filename parameter. |
Getdevicecapabilities |
Retrieves information about a specified device. |
Getdevicefileattributes |
Retrieves the properties of a specified device file. |
Getdevicefilesize |
The size of the device file, in bytes. |
Getdevicefiletime |
The date and time when the device file is retrieved. |
Getdevicememorystatus |
Retrieves memory usage information of a device. |
Getdevicesystemfolderpath |
The path to the system folder of the device. |
Getdevicesysteminfo |
Retrieves the system details of a device. |
Getdevicesystempowerstatus |
Retrieves the power status of a device. |
Getdeviceversion |
Search for the operating system version of the device. |
Movedevicefile |
Move or rename an existing device file to a new location. |
Removedevicedirectory |
Delete the directory from the device. |
Setdevicefileattributes |
Set the properties of files on the device. |
Setdevicefiletime |
Set the date and time of the file in the device. |
Back to Top using rapi
To simplify the use of rapi classes, C # addedUsingStatement, added in the VB. NET code exampleImportsStatement:
[VC#.NET]using OpenNETCF.Desktop.Communication;[VB.NET]Imports OpenNETCF.Desktop.Communication
In addition, a module variable is declared.MyrapiUsed to store rapi instances.
[VC #. Net] // declare the instance of the rapi object. Rapi myrapi; [VB. NET] 'declares the instance of the rapi object. Dim withevents myrapi as new rapi
Back to Top connect to devices
When using the rapi method, a desktop application establishes a connection with the device.
Note:Using rapi in desktop applications requires activation of ActiveSync connections between PCs and devices.
The application example in this article establishes a connection with the device in the form_load event. To connect to a device, useConnectMethod. As shown in the following code, 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 'to 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 () loopcatch ex as 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 established, you are ready to use the functions provided by rapi. First, learn how to manage the directory files of a device through a desktop application.
Back to Top User Files
Rapi provides many file directory processing functions. Three file-related functions are demonstrated here: copying a file to a device, copying a file from a device, moving a file from a device, and deleting a file from a device. First, copy the file.
Copy a file to a device or from a device
One of the simplest ways to move data into or out of a device is to copy text or XML files between the PC and the device. Figure 1 shows how to use the rapi demo program. In mobile applications, text and XML-based files can be used as a simple way to store application data or configuration data.
Figure
1
:
Rapi
Demo program
"
Copy a file
"
Tab
The rapi class of the opennetcf. desktop. Communication namespace provides two methods to copy files:CopyfiletodeviceAndCopyfilefromdevice. Both methods regard the source file as the first variable and the target file as the second variable.
The btncopyperform button click event process to implement these two methods. CallCopyfiletodeviceMethod or callCopyfilefromdeviceThe method depends on the command selected in the user interface combo box.
[VC #. Net] private void btncopypolicm_click (Object sender, system. eventargs e) {// execute replication. 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 (txtcopys Ource. text, txtcopydestination. text); MessageBox. show ("your file has been copied. "," Copy success "); break; Case" from device to desktop ": myrapi. copyfilefromdevice (txtcopysource. text, txtcopydestination. text); MessageBox. show ("your file has been copied. "," Copy success "); break ;}}// process all possible errors. Catch (exception ex) {MessageBox. show ("the following error occurred copying the file-" + ex. message, "Copy error");} [VB. net] private sub btncopy1_m_click (byval sender as system. object, byval e as system. eventargs) handles btncopyperform. click 'Copy. Try if (txtcopysource. TEXT = "") or (txtcopydestination. TEXT = "") Then MessageBox. show ("You must provide both a source and destination file. ", _" missing file information ") Exit sub end if select case cmbcopydirection. text case "" MessageBox. show ("You must select a direction before initiating the copy. ", _" no destination selected ") Exit sub case" from desktop to device "myrapi. copyfiletodevice (T Xtcopysource. text, txtcopydestination. text) Case "from device to desktop" myrapi. copyfilefromdevice (txtcopysource. text, txtcopydestination. text) end select MessageBox. show ("your file has been copied. "," Copy success ") 'handles all possible errors. Catch ex as exception MessageBox. Show ("the following error occurred copying the file-" & Ex. Message, _ "Copy error") end tryend sub
Files in mobile devices
Sometimes you may need to move or rename files on the device. For example, you may need to back up the configuration file before copying the new version to the device.2 shows how to use the rapi demo program.
Figure
2
:
Rapi
Demo program
"
Move files
"
Tab
The rapi class of the opennetcf. desktop. Communication namespace providesMovedevicefileTo move or rename the file. Like the copyfile method, this method regards the source file as the first variable and the target file as the second variable.
BtnmoveperformMovedevicefileMethod.
[VC #. Net] private void btnmoveperform_click (Object sender, system. eventargs e) {// execute 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 all possible errors. 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 '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 ") 'handles all possible errors. Catch ex as exception MessageBox. Show ("the following error occurred moving the file-" & Ex. Message, _ "Move error") end tryend sub
Delete files from a device
Most of the time, you will find that the rapi file copying method must be used together with the file deletion method. For example, you may require a desktop application to copy the files used by the application to store device data, and then return and delete the files from the device after the copy task is completed successfully, so that the mobile application can collect new data again.Figure 3Shows how to use the rapi demo program.
Figure
3
:
Rapi
Demo program
"
Delete an object
"
Tab
The rapi class of the opennetcf. desktop. Communication namespace providesDeletedevicefileMethod to delete a device file. The file to be deleted is the first variable of this method.
BtndeleteperformDeletedevicefileMethod.
[VC #. Net] private void btndeleteperform_click (Object sender, system. eventargs e) {// Delete the object. 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 all possible errors. 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 'to delete the file. 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 ") 'handles all possible errors. Catch ex as exception MessageBox. Show ("the following error occurred while deleting the file-" & Ex. Message, _ "delete error") end tryend sub
We have discussed three common file-related methods, namely, copying, moving, and deleting methods. Next let's take a look at how to start the application on the device from the desktop application.
Back to Top start the application
There are various reasons why you want to start applications on your device from desktop applications. With this technology, you can:
Install the new application. You only need to copy the cab file to the device, and then run the cab installer in the device to perform the installation. This technology is useful if you want to automate the delivery and installation of application updates.
Note: : Another method is to use the built-in functions of ActiveSync to automatically process the installation process on the desktop.
After installing the new application version, restart the mobile application on the device.
After the device application is started, the system processes new update data stored in a text file or XML file.
Figure 4Shows how to use the rapi demo program.
Figure
4
:
Rapi
Demo program
"
Start the application
"
Tab
The rapi class of the opennetcf. desktop. Communication namespace providesCreateProcessMethod to start the device file. The device application to be started is the first variable of this method. You can use the command line to be processed by the application as the second variable of the method.
The btnlaunchperform button is clicked to implement the event process.CreateProcessMethod.
[VC #. Net] private void btnlaunchcmdm_click (Object sender, system. eventargs e) {// execute startup. Try {If (txtlaunchfile. TEXT = "") {MessageBox. show ("You must provide a file to launch. "," No file provided ");} else {myrapi. createProcess (txtlaunchfile. text, txtlaunchcommand. text); MessageBox. show ("your file has been launched. "," launch success ") ;}// handle all possible errors. Catch (exception ex) {MessageBox. show ("the following error occurred while launching the file-" + ex. message, "launch error");} [VB. net] private sub btnlaunch1_m_click (byval sender as system. object, byval e as system. eventargs) handles btnlaunchperform. click 'Start. Try if (txtlaunchfile. TEXT = "") Then MessageBox. show ("You must provide a file to launch. ", _" no file provided ") Exit sub end if myrapi. createProcess (txtlaunchfile. text, txtlaunchcommand. text) MessageBox. show ("your file has been launched. "," launch success ") 'handles all possible errors. Catch ex as exception MessageBox. Show ("the following error occurred while launching the file-" & Ex. Message, _ "launch error") end tryend sub
Next, we will discuss the last rapi-related topic, that is, retrieving system information. As you can see in the following sections, the rapi class provides several methods to retrieve information about connected devices.
Back to Top
If you can retrieve the specific system information of the connected device, the desktop application can provide the content or change function based on the following information:
The processor used to connect the device (often responding when pushing the cab file to a device that contains a specific processor file ).
Note: : This technique is useful if you deploy applications to an earlier version of the Pocket PC device. Because Windows Mobile devices are based on ARM processors.
The operating system version running on the connection device (similar to the processor type, which is often used to provide the corresponding file update ).
The power status of the connected device (used to warn the user to check whether the power supply of the device is too low before use ).
Memory status of the connected device (used to determine whether the data is detached. If the user loads an unauthorized application or other memory-related functions, use it to determine whether there is sufficient space to install and update the application ).
Figure 5Shows how to use the rapi demo program.
Figure
5
:
Rapi
Demo program
"
Device information
"
Tab
The following four information retrieval methods are provided by the rapi:Getdevicesysteminfo(Processor type ),Getdeviceversion(Operating system version ),Getdevicesystempowerstatus(Power status) andGetdevicememorystatus(Memory ). The clicking event process of the btninforetrieve button implements each method.
[VC #. Net] private void btninforetrieve_click (Object sender, system. eventargs e) {string Info; memorystatus MS; system_info Si; system_power_status_ex SPS; osversioninfo VI; // retrieves system information. Myrapi. getdevicesysteminfo (Out Si); // retrieve the OS version number of the device. Myrapi. getdeviceversion (Out VI); // retrieve the power status of the device. Myrapi. getdevicesystempowerstatus (Out SPS); // retrieves the device memory status. Myrapi. getdevicememorystatus (Out MS); // sets the format of the retrieved information. Info = "the connected device has an"; Switch (SI. wprocessorarchitecture) {Case processorarchitecture. intel: info + = "Intel processor. /n "; break; Case processorarchitecture. MIPs: info + = "MIPS processor. /n "; break; Case processorarchitecture. arm: info + = "ARM processor. /n "; break; default: info =" unknown Processor type. /n "; break;} info + =" OS version: "+ VI. dwmajorversion + ". "+ VI. dwminorve Rsion + ". "+ VI. dwbuildnumber + "/N"; if (SPs. aclinestatus = 1) {info + = "on AC power: yes/N";} else {info + = "on AC power: No/N ";} info + = "battery level:" + SPs. batterylifepercent + "%/N"; Info + = "total memory:" + String. format ("{0 :###,###,###}", Ms. dwtotalphys) + "/N"; // display the result. Lblinfo. TEXT = Info;} [VB. net] private sub btninforetrieve_click (byval sender as system. object, byval e as system. eventargs) handles btninforetrieve. click dim info as string dim MS as new memorystatus dim Si as new system_info dim SPS as new system_power_status_ex dim VI as new osversioninfo 'to retrieve system information. Myrapi. getdevicesysteminfo (SI. Myrapi. getdeviceversion (vi) 'retrieves the power status of the device. Myrapi. getdevicesystempowerstatus (SPS) 'to retrieve the device memory status. Myrapi. getdevicememorystatus (MS) 'sets the format of the retrieved information. Info = "the connected device has an" select case Si. wprocessorarchitecture case processorarchitecture. intel info + = "Intel processor. "& vbcrlf case processorarchitecture. MIPs info + = "MIPS processor. "& vbcrlf case processorarchitecture. arm info + = "ARM processor. "& vbcrlf case else info =" unknown Processor type. "& vbcrlf end select info + =" OS version: "& VI. dwmajorversion &". "& VI. dwmino Rversion &". "& VI. dwbuildnumber & vbcrlf info + = "on AC power:" & IIF (SPs. aclinestatus = 1, "yes", "no") & vbcrlf info + = "battery level:" & SPs. batterylifepercent & "%" & vbcrlf info + = "total memory:" & string. format ("{0 :###,###,###}", Ms. dwtotalphys) & vbcrlf. Lblinfo. Text = infoend sub
The above is a brief introduction to remote APIs and instructions on how to merge desktop applications into mobile solutions. It is recommended that you take some time to learn about the many other functions provided by the opennetcf. desktop. Communication namespace. Remember that these methods and the opennetcf namespace provide a variety of options to enhance the performance of handheld applications.
Back to Top. NET Compact framework
Do you want to quickly learn about. NET Compact framework in a week? The purpose can be achieved by attending the training here. Now you have a five-day netcf training course, from which you can quickly learn everything you want to know. The training course will show you how to use. NET Compact framework, SQL Server ce, and XML to create effective mobile solutions. Please visit the address below for a detailed training outline: http://www.larryroof.com/(English ).
If you cannot attend a face-to-face course, you can purchase a new book from apress: the definitive guide to the. NET Compact framework (English ). This includes a comprehensive overview of how to use the. NET Compact framework to create a mobile application.
Back to Top
This will be discussed later. Spring training is just around the corner. I want to bring my slide, My Pocket PC, and my questions to the beautiful Florida in yangguan. In the next article, I will explain more methods to mobile technology developers.
Back to Top