Click here to Skip to main content
15,867,453 members
Articles / Mobile Apps / Windows Mobile

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 96K   1.6K   52   24
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.

    C#
    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\\*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.

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:

C#
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:

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

Delete a file on the Handheld:

C#
public void DeleteFile(string deviceFilePath)
{
    if (Rapi.DeviceFileExists(deviceFilePath))
    {
        Rapi.DeleteDeviceFile(deviceFilePath);
    }
}

Copy files recursively from, including the directory structure.

C#
//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:

C#
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)


Written By
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 7:43
Patrick Harris30-Jul-12 7:43 
GeneralGreat but Error while copyiong Pin
SchreiberManolo21-Jun-10 0:15
SchreiberManolo21-Jun-10 0:15 
Generalmaybe bug- in CopyFilesFromDevice Pin
bmnot15-Feb-10 6:52
bmnot15-Feb-10 6:52 
Generalprobably need a different way Pin
Member 83331212-Oct-09 7:47
Member 83331212-Oct-09 7:47 
GeneralThanks, very helpful! Pin
Jim Maguire15-Jul-09 14:40
Jim Maguire15-Jul-09 14:40 
Very similar to what I'm trying to do, this gives me a real head start! Big Grin | :-D
GeneralThanks Buddy Pin
divyesh143221-Jan-09 20:29
divyesh143221-Jan-09 20:29 
GeneralMany thanks! Pin
ConfusedCoder23-Sep-08 5:45
ConfusedCoder23-Sep-08 5:45 
GeneralThanks Pin
Arijit_911-Sep-08 7:27
Arijit_911-Sep-08 7:27 
GeneralRe: Thanks Pin
Paul Brower11-Sep-08 7:47
Paul Brower11-Sep-08 7:47 
GeneralExcellent Article !! Pin
Braulio Dez3-Jul-08 0:00
Braulio Dez3-Jul-08 0:00 
GeneralThanks! Pin
vitoravelino25-May-08 15:04
vitoravelino25-May-08 15:04 
GeneralCould be useful Pin
Alex_122-May-08 18:34
Alex_122-May-08 18:34 
Questioncan't find PInvoke DLL 'rapi.dll' Pin
Christopher M. Sanyk16-Apr-08 8:48
Christopher M. Sanyk16-Apr-08 8:48 
GeneralRe: can't find PInvoke DLL 'rapi.dll' Pin
Christopher M. Sanyk16-Apr-08 10:29
Christopher M. Sanyk16-Apr-08 10:29 
QuestionRe: can't find PInvoke DLL 'rapi.dll' Pin
Yendi14-Jan-09 10:56
Yendi14-Jan-09 10:56 
AnswerRe: can't find PInvoke DLL 'rapi.dll' Pin
MichP20-Mar-09 8:55
MichP20-Mar-09 8:55 
GeneralEnum Files Pin
pankajawasthi9910-Mar-08 13:33
pankajawasthi9910-Mar-08 13:33 
QuestionThx Paul, But I just wondered [modified] Pin
taino25-Sep-07 23:26
taino25-Sep-07 23:26 
AnswerRe: Thx Paul, But I just wondered Pin
Paul Brower26-Sep-07 0:58
Paul Brower26-Sep-07 0:58 
GeneralLook no further, CF.Remoting is here... Pin
CF User20-Aug-07 16:23
CF User20-Aug-07 16:23 
GeneralRe: Look no further, CF.Remoting is here... Pin
Patrick Harris26-Jul-12 18:02
Patrick Harris26-Jul-12 18:02 
GeneralThanks Paul - nice util Pin
DrPikz11-Apr-07 6:01
DrPikz11-Apr-07 6:01 
GeneralRe: Thanks Paul - nice util Pin
Paul Brower11-Apr-07 7:30
Paul Brower11-Apr-07 7:30 
GeneralRe: Thanks Paul - nice util Pin
DrPikz11-Apr-07 21:15
DrPikz11-Apr-07 21: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.