Click here to Skip to main content
Click here to Skip to main content

Manage Handheld Device Files from Desktop

, 23 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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.Connect();
    rapi.Disconnect();
  • 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\\*xml This will return all XML files within Folder1 in the FileList array.
    My Documents\\Folder1\\123.xml This will return one item, 123.xml in the FileList array.

HHFiles.cs

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
{
    get
    {
         return rapi.EnumFiles("My Documents");
    }
}

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

Example:

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

Delete a file on the Handheld:

public void DeleteFile(string deviceFilePath)
{
    if (Rapi.DeviceFileExists(deviceFilePath))
    {
        Rapi.DeleteDeviceFile(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)
  {
      CreateLocalStartingDirectory(localStartingDirectory);
        FileList deviceDirectory = GetFileList(deviceStartingDirectory);            
        if (deviceDirectory == null || deviceDirectory.Count != 1)
        {
            throw new System.IO.FileNotFoundException("Invalid Device Directory", 
                      deviceStartingDirectory);
        }

        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;
                    CopyFilesFromDevice(newLocalDirectory,
                        newDeviceDirectory,fileMask,includeSubDirectories,overwrite);
                }
                else
                {
                    if (!MatchesFileMask(dirInfo.FileName, fileMask)) continue;
                    string newDeviceFile =
                        deviceStartingDirectory + "\\" + dirInfo.FileName;
                    string newLocalFile =
                        localStartingDirectory + "\\" + dirInfo.FileName;
                    CopyFileFromDevice(newLocalFile,newDeviceFile,overwrite);
                }
            }
        }

Example:

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

Conclusion

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.

License

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

Share

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.
Follow on   LinkedIn

Comments and Discussions

 
GeneralBroken Link PinmemberPatrick Harris30-Jul-12 8:43 
GeneralGreat but Error while copyiong PinmemberSchreiberManolo21-Jun-10 1:15 
Generalmaybe bug- in CopyFilesFromDevice Pinmemberbmnot15-Feb-10 7:52 
Generalprobably need a different way PinmemberMember 83331212-Oct-09 8:47 
GeneralThanks, very helpful! PinmemberJim Maguire15-Jul-09 15:40 
GeneralThanks Buddy Pinmemberdivyesh143221-Jan-09 21:29 
GeneralMany thanks! PinmemberConfusedCoder23-Sep-08 6:45 
GeneralThanks PinmemberMember 208669011-Sep-08 8:27 
GeneralRe: Thanks PinmemberPaul Brower11-Sep-08 8:47 
GeneralExcellent Article !! PinmemberBraulio Dez3-Jul-08 1:00 
GeneralThanks! Pinmembervitoravelino25-May-08 16:04 
GeneralCould be useful PinmemberAlex_122-May-08 19:34 
Questioncan't find PInvoke DLL 'rapi.dll' PinmemberChristopher M. Sanyk16-Apr-08 9:48 
GeneralRe: can't find PInvoke DLL 'rapi.dll' PinmemberChristopher M. Sanyk16-Apr-08 11:29 
QuestionRe: can't find PInvoke DLL 'rapi.dll' PinmemberYendi14-Jan-09 11:56 
AnswerRe: can't find PInvoke DLL 'rapi.dll' PinmemberMichP20-Mar-09 9:55 
GeneralEnum Files Pinmemberpankajawasthi9910-Mar-08 14:33 
QuestionThx Paul, But I just wondered [modified] Pinmembertaino26-Sep-07 0:26 
AnswerRe: Thx Paul, But I just wondered PinmemberPaul Brower26-Sep-07 1:58 
GeneralLook no further, CF.Remoting is here... PinmemberCF User20-Aug-07 17:23 
GeneralRe: Look no further, CF.Remoting is here... PinmemberPatrick Harris26-Jul-12 19:02 
GeneralThanks Paul - nice util PinmemberDrPikz11-Apr-07 7:01 
GeneralRe: Thanks Paul - nice util PinmemberPaul Brower11-Apr-07 8:30 
GeneralRe: Thanks Paul - nice util PinmemberDrPikz11-Apr-07 22:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 23 Mar 2007
Article Copyright 2007 by Paul Brower
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid