Click here to Skip to main content
15,125,854 members
Articles / Mobile Apps / Windows Mobile
Posted 23 Mar 2007


52 bookmarked

Manage Handheld Device Files from Desktop

Rate me:
Please Sign up or sign in to vote.
4.75/5 (16 votes)
23 Mar 2007CPOL3 min read
Using the OpenNetCF RAPI wrapper class to manage files on a Handheld Device running the Windows Mobile OS.

Introduction & Background

I recently worked on an application that utilizes the .NET Compact Framework 2.0. We have an application counterpart that is installed on the Desktop, which manages sending files originally created on Handheld Devices over a B2B service.

We have had file-corruption problems in the past involving Microsoft ActiveSync. Let me make it clear that I am not stating that ActiveSync was directly responsible for the problems. We have a unique requirement that requires us to perform a second type of synchronization, which is between a local working folder and the synchronized files from the Handheld Device which resides on the desktop computer.

Screenshot - Sync.jpg

As I started strategizing on how to approach dealing with these synchronization conflicts, I realized something: we don't actually need to synchronize files, we just need to copy them! Due to the way we have designed our software, various XML files are created on the Handheld Devices, and then our Desktop software takes those XML files, sometimes tweaking the data, and then produces data files that it transmits over a B2B service.

So I got to thinking. What if I just yanked the files directly off the Handheld Device? Performing this activity was new to me, information on this topic was hard to find, and that is what prompted me to write this article!

Note: This code has only been tested for Window Mobile 5 devices.

Google, CodeProject, and OpenNetCF

Believe it or not, for someone (me) who has not done it before, figuring out how to find and manage files on a Handheld Device from a desktop application was not easy. Google, Google Groups, CodeProject, and a myriad of other sites were not helpful. The best I could find was articles for managing a known file. I needed to be able to enumerate an entire directory structure on the Handheld Device.

I then came across OpenNetCF (OpenNetCF[^]). The have an Open Source DLL called OpenNETCF.Desktop.Communication.dll. This can be downloaded here[^].

Although OpenNetCf.Desktop.Communication.dll is very handy, I found it a bit difficult to work with. I like to see examples of how to use objects. I decided to write a kind of wrapper class around this DLL, simplifying all the tasks I needed to perform with Handheld Device files. Those tasks are listed in the following tests:

Screenshot - Sync.jpg

OpenNETCF.Desktop.Communication Assembly

RAPI (Remote API) via OpenNETCF.Desktop.Communication.dll is what allows us to work with the Handheld Device. I will touch lightly on some of the methods and properties available in this assembly.

  • RAPI.DevicePresent

    This property returns a boolean, and is true if a device is connected to your computer.

  • RAPI.Connect(), RAPI.Disconnect()

    Prior to working with your device, you must connect to it via RAPI. When you're done, you should disconnect.

    RAPI rapi = new RAPI();
  • RAPI.EnumFiles

    FileList RAPI.EnumFiles(string fileName) is what allows us to get files and directories from the Handheld Device. What's a little confusing is the string fileName part. The following are valid string arguments which can be passed into the RAPI.EnumFiles method:

    My Documents\\Folder1\\This will return one item, Folder1, in the FileList array.
    My Documents\\Folder1\\*This will return all files and directories within Folder1 in the FileList array.
    My Documents\\Folder1\\*xmlThis will return all XML files within Folder1 in the FileList array.
    My Documents\\Folder1\\123.xmlThis will return one item, 123.xml in the FileList array.


I've included my HHFiles class here. The following are some code snippets from this class.

Access the My Documents directory on the Handheld:

public FileList MyDocuments
         return rapi.EnumFiles("My Documents");

//Copy file from device to desktop.
public void CopyFileFromDevice(string localFilePath, 
            string deviceFilePath, bool overwrite)


HHFiles files = new HHFiles();
      @"My Documents\Folder\Somefile.txt,true);

Delete a file on the Handheld:

public void DeleteFile(string deviceFilePath)
    if (Rapi.DeviceFileExists(deviceFilePath))

Copy files recursively from, including the directory structure.

//fileMask - file extension. (i.e., "xml", "doc", "xls")
//includSubDirectories - recurse sub folders (true/false)
//overwrite - overwrite if target file exists (true/false) 

public void CopyFilesFromDevice(string localStartingDirectory, 
  string deviceStartingDirectory, 
  string fileMask, 
  bool includeSubDirectories, 
  bool overwrite)
        FileList deviceDirectory = GetFileList(deviceStartingDirectory);            
        if (deviceDirectory == null || deviceDirectory.Count != 1)
            throw new System.IO.FileNotFoundException("Invalid Device Directory", 

        FileList directoryList = GetFileList(deviceStartingDirectory + "\\*");

            foreach (FileInformation dirInfo in directoryList)
                if (dirInfo.FileAttributes == (int) FileAttributes.Directory )
                    if (!includeSubDirectories) continue;

                    string newDeviceDirectory =
                        deviceStartingDirectory + "\\" + dirInfo.FileName;
                    string newLocalDirectory =
                        localStartingDirectory + "\\" + dirInfo.FileName;
                    if (!MatchesFileMask(dirInfo.FileName, fileMask)) continue;
                    string newDeviceFile =
                        deviceStartingDirectory + "\\" + dirInfo.FileName;
                    string newLocalFile =
                        localStartingDirectory + "\\" + dirInfo.FileName;


HHFiles files = new HHFiles();
files.CopyFilesFromDevice(@"c:\temp", @"My Documents\Work","xml",true,true);


Well, it's been a little while since I wrote an article. I hope that you find this useful. I spent a lot of time trying to figure out how to deal with files on a Handheld Device, and this class has greatly simplified the work. If anything is unclear, or you have any question, I'll check back frequently, so please just post your question.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul Brower
Choice Genetics US
United States United States
Seasoned IT Professional. Currently the IT Director for Choice Genetics, and also the world-wide manager for IT Projects related to R&D for Groupe Grimaud (our parent company).

I've spent about half my career as a contractor. I've lived all over the place, but currently reside near St. Louis, Missouri. Moved out here from Roseville, California in Feb-2005. No regrets yet.

Over the recent years I've written software for:
- Disposing of radioactive and toxic waste.
- Disposing of surplus inventory during the Decommission of McClellan Air Force Base.
- Facilitating genetic improvement for Swine Breeding.
- Managing children placed in State custody.
- Dealing with commercial trucking delivery schedules.
- Tracking high resolution images from archeological digs.
- Project Management for the roofing industry.
- Processing engines for credit card transactions.

Comments and Discussions

GeneralBroken Link Pin
Patrick Harris30-Jul-12 8:43
MemberPatrick Harris30-Jul-12 8:43 
GeneralGreat but Error while copyiong Pin
SchreiberManolo21-Jun-10 1:15
MemberSchreiberManolo21-Jun-10 1:15 
Generalmaybe bug- in CopyFilesFromDevice Pin
bmnot15-Feb-10 7:52
Memberbmnot15-Feb-10 7:52 
Generalprobably need a different way Pin
Member 83331212-Oct-09 8:47
MemberMember 83331212-Oct-09 8:47 
GeneralThanks, very helpful! Pin
Jim Maguire15-Jul-09 15:40
MemberJim Maguire15-Jul-09 15:40 
GeneralThanks Buddy Pin
divyesh143221-Jan-09 21:29
Memberdivyesh143221-Jan-09 21:29 
GeneralMany thanks! Pin
ConfusedCoder23-Sep-08 6:45
MemberConfusedCoder23-Sep-08 6:45 
GeneralThanks Pin
Arijit_911-Sep-08 8:27
MemberArijit_911-Sep-08 8:27 
GeneralRe: Thanks Pin
Paul Brower11-Sep-08 8:47
MemberPaul Brower11-Sep-08 8:47 
GeneralExcellent Article !! Pin
Braulio Dez3-Jul-08 1:00
MemberBraulio Dez3-Jul-08 1:00 
There is no quite much info avaialble about RAPI and this excellent wrapper (OpenNetCF).

Your article makes quite easy to start working with this library.


/// -------------------------
     Braulio Díez
/// -------------------------

GeneralThanks! Pin
vitoravelino25-May-08 16:04
Membervitoravelino25-May-08 16:04 
GeneralCould be useful Pin
Alex_122-May-08 19:34
MemberAlex_122-May-08 19:34 
Questioncan't find PInvoke DLL 'rapi.dll' Pin
Christopher M. Sanyk16-Apr-08 9:48
MemberChristopher M. Sanyk16-Apr-08 9:48 
GeneralRe: can't find PInvoke DLL 'rapi.dll' Pin
Christopher M. Sanyk16-Apr-08 11:29
MemberChristopher M. Sanyk16-Apr-08 11:29 
QuestionRe: can't find PInvoke DLL 'rapi.dll' Pin
Yendi14-Jan-09 11:56
MemberYendi14-Jan-09 11:56 
AnswerRe: can't find PInvoke DLL 'rapi.dll' Pin
MichP20-Mar-09 9:55
MemberMichP20-Mar-09 9:55 
GeneralEnum Files Pin
pankajawasthi9910-Mar-08 14:33
Memberpankajawasthi9910-Mar-08 14:33 
QuestionThx Paul, But I just wondered [modified] Pin
taino26-Sep-07 0:26
Membertaino26-Sep-07 0:26 
AnswerRe: Thx Paul, But I just wondered Pin
Paul Brower26-Sep-07 1:58
MemberPaul Brower26-Sep-07 1:58 
GeneralLook no further, CF.Remoting is here... Pin
CF User20-Aug-07 17:23
MemberCF User20-Aug-07 17:23 
GeneralRe: Look no further, CF.Remoting is here... Pin
Patrick Harris26-Jul-12 19:02
MemberPatrick Harris26-Jul-12 19:02 
GeneralThanks Paul - nice util Pin
DrPikz11-Apr-07 7:01
MemberDrPikz11-Apr-07 7:01 
GeneralRe: Thanks Paul - nice util Pin
Paul Brower11-Apr-07 8:30
MemberPaul Brower11-Apr-07 8:30 
GeneralRe: Thanks Paul - nice util Pin
DrPikz11-Apr-07 22:15
MemberDrPikz11-Apr-07 22:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.