none
Copy file from pc to Windows Mobile 6.1 device

    Question

  • Hello

    I have the following setup

    Development: Microsoft Visual Studio 2008 Pro on Vista.  Windows Mobile Development SDK

    Windows Mobile 6.1 Professional mobile dcevice

    I need to copy a file (.sdf) from my pc to my mobile device using VB.net.

    There is (as usual) a mine of confusing information on the net regarding using RAPI and I do not know where to start.

    I simply want to copy a file from pc to mobile device and back (simple!?)

    Can anyone out there help please?

    Many thanks in advance.


    With thanks, NECoder
    Tuesday, March 01, 2011 12:06 PM

Answers

  • Thanks for all your help everyone.  Created a forms app in Visual Studio and added some RAPI code to achieve my end goal.  My problems was I was trying to run the code from the device - as Ilya pointed out above - RAPI is designed to run from desktop only.

    Solutiuon:

    create new vb.net forms app

    add RAPI2.dll reference (get RAPI2 from Codeplex.com)

    In form import System.devices namespace

    Here's a dump of my code for anyone who needs it (note -change file references to something meaningful to you) :-

     

    Imports

     

    System.Devices

    Public

     

    Class Form1

     

    Dim dev As RemoteDevice

     

    Dim mgr As New System.Devices.RemoteDeviceManager

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    dev = mgr.Devices.FirstConnectedDevice

     

    If dev Is Nothing Then

    MsgBox(

    "No device connected")

     

    Else

    MsgBox(

    "Connected to: " & dev.Name)

     

    End If

     

    End Sub

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    Dim RAPIfile As RemoteFile

    RAPIfile.CopyFileToDevice(dev,

    "C:\Users\Ian\Documents\test.txt", "\\my documents\test.txt", True)

    RAPIfile =

    Nothing

     

    End Sub

     

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

     

    Dim RAPIfile As RemoteFile

    RAPIfile.CopyFileFromDevice(dev,

    "\\program files\ie_mobile\client.sdf", "C:\Users\Ian\Documents\client.sdf", True)

    RAPIfile =

    Nothing

     

    End Sub

    End

     

    Class


    With thanks, NECoder
    • Marked as answer by NECoder Thursday, March 03, 2011 8:22 AM
    Thursday, March 03, 2011 8:21 AM

All replies

  • And another thing - do I really need to use a 3rd party component such as RAPI - can I not achieve this with .net libraries (eg SYSTEM.IO) ?
    With thanks, NECoder
    Tuesday, March 01, 2011 1:59 PM
  • RAPI is not 3rd party. It is from Microsoft. You can use CeCopyFile to accomplish your task.

    You can also use a wrapper. For the wrapper, you may use something like this from CodePlex: http://rapi2.codeplex.com/

    Or the popular OpenNetCF Desktop Communications library: http://www.opennetcf.com/FreeSoftware/DesktopCommunication/tabid/90/Default.aspx

    I have a CP article covering RAPI2 in C++ you may find useful: http://www.codeproject.com/KB/windows/CeDir.aspx

    You can use the System.IO libraries, but it is more difficult than using RAPI. You can write a server program (like FTP) that runs on the device and waits for files to be sent to it and and FTP client for the PC that send files over the ActiveSync network connection. Actually, you can just use a pre-made FTP client/server for this purpose, if you want.

    -PaulH

    Tuesday, March 01, 2011 8:10 PM
  • I forgot to say I am a VB.net programmer.  RAPI2 looks good. Are there any VB.NET examples of how to implement?

    Thank you.


    With thanks, NECoder
    Wednesday, March 02, 2011 10:16 AM
  • Looking for further examples on line and CeCopyFile looks to be what I need but I cannnot work out how to implement this in VB.net as there are references to pointers etc???  This must be sooooo simple (if you know how)

    Any help always very greatfully received


    With thanks, NECoder
    Wednesday, March 02, 2011 11:11 AM
  • You will need to P/Invoke the native function if there isn't a VB wrapper available. I'm not a VB.NET person, so I can't help you with specific code, but these two links look useful:
    http://www.codeproject.com/KB/dotnet/PInvoke.aspx
    http://www.google.com/search?q=vb.net+pinvoke

    Also, I misread your original question. CeCopyFile will allow you to copy files from one location on the device to another location on the device. If you want to copy from the PC to the device, you will need to use CeCreateFile, CeWriteFile, and CeCloseFile.
    http://msdn.microsoft.com/en-us/library/ms913647.aspx

    -PaulH

    Wednesday, March 02, 2011 2:33 PM
  • Paul, Thanks for your reply.

    Having looked at the OpenNetCF option I am experimenting with this. Downloaded and built the assembly.

    I have included a reference to OpenNETCF.Desktop.Communication in my vb.net mobile device project.

    Only problem is now I get a "Can't find PInvoke DLL 'rapi.dll'" error.  I know you're not a vb person but here's my code:

    Imports

     

    OpenNETCF.Desktop.Communication

    Module

     

    FileOps

     

    Dim rapi As New RAPI

     

    ' Initialize RAPI

     

    Public Function ConnectRapi() As Long

    rapi.Connect()

     

    End Function

     

    ' Uninitialize RAPI

     

    Public Function DisconnectRapi() As Long

    rapi.Disconnect()

     

    End Function

     

     

    Public Sub xFerFile()

    rapi.CopyFileToDevice(

    "c:\my documents\test.text", _

     

    "my device\program files\ie_mobile\test.text")

     

    End Sub

     

    End

     

    Module

    Rapi.connect fails with the Pinvoke error.

    Trawling throught the net, others say it the RAPI.dll must reside in the program files or windows folder on the device, which it does as it is getting copied over when deploying my solution.

    I am at a complete loss as to how something so simple as copying a file from pc to device can be so difficult??


    With thanks, NECoder
    Wednesday, March 02, 2011 3:04 PM
  • Do you have the Windows Mobile Device Center for Vista installed?

    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=46f72df1-e46a-4a5f-a791-09f07aaa1914&displayLang=en

     

    You will need that to communicate with the device. It should be in C:\Windows\System32

    -PaulH

    Wednesday, March 02, 2011 3:36 PM
  • Yes, I have that installed, although I was getting problems with "Not Connected" but that's an asside issue (within visual studio it works fine)

     


    With thanks, NECoder
    Wednesday, March 02, 2011 3:53 PM
  • Can you verify that c:\windows\system32\rapi.dll exists on your system?

    You're running this executable on your PC and not your mobile device, correct?

    -PaulH

    Wednesday, March 02, 2011 3:57 PM
  • Yes rapi.dll is on my Vista machine.  When I run the mobile application, the mobile emulator (or my actual mobile device if connected) runs my program. rapi.dll is copied over to the emulator/device on debug.

     


    With thanks, NECoder
    Wednesday, March 02, 2011 4:14 PM
  • RAPI applications need to run on your PC not the mobile device. RAPI.dll is compiled for x86 Windows, not Windows Mobile, so it will not work on your mobile device.

    The PC application communicates with a RAPI service that is always running on your mobile device and comes pre-installed with Windows Mobile. 

    -PaulH

    Wednesday, March 02, 2011 4:21 PM
  • My end goal is to copy a file (sql ce database) from a computer to the mobile device.  Surely this must be achieveble?

    All articles I have seen so far seem to suggest this is achieveable via the OpenNetCF Desktop Communications library.

    Thanks for all you help anyway.  Maybe any VB.net gurus could help.


    With thanks, NECoder
    Wednesday, March 02, 2011 4:29 PM
  • This is possible. You must write a PC based application that uses the OpenNetCF Desktop Communications Library.

    Here is an example of doing this in VB.NET:

    http://stackoverflow.com/questions/277529/rapi-copy-file-using-opennetcf-desktop-communication-dll

    -PaulH

    Wednesday, March 02, 2011 4:33 PM
  • Yes but we are back to square one - my code gives the Can't find PInvoke DLL 'rapi.dll' error when trying the rapi connect.  :(

     


    With thanks, NECoder
    Wednesday, March 02, 2011 4:50 PM
  • Are you still trying to run your application on the mobile device or have you changed it to run on your Windows PC?

    -PaulH

    Wednesday, March 02, 2011 5:06 PM
  • The application is designed to run on the mobile device and when connected, copy a database from the pc to the mobile device (and then later copy it back again).  Thanks.
    With thanks, NECoder
    Wednesday, March 02, 2011 7:29 PM
  • You can not do that using RAPI as it only only allows accessing device from the desktop side and not desktop from device which is what you need.

    To access desktop from device you can use file shares accessible from device via UNC paths (\\computer\share\path), web server running on device or other custom means of communication.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, March 02, 2011 8:59 PM
  • Hello Ilya, thanks for your input.

    RAPI2 suggests that you can copy files back and fourth - from a pc to mobile to pc (via activeSync / WMDC)

    rrapi2.system.devices.remoteFile.CopyFileToDevice

    rrapi2.system.devices.remoteFile.CopyFileFromDevice

    I would like to see this used in a VB.net example as I cannot understand C# !!


    With thanks, NECoder
    Wednesday, March 02, 2011 10:15 PM
  • RAPI is a desktop only API. It is indeed allow moving files both ways (among other things it can do) but that is done by the application running on the desktop, not by the application running on the device. If you need to do something from the application running on device then RAPI is simply not an option, whatever in VB or C#.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, March 03, 2011 5:38 AM
  • OK, thanks for clarifying that.  I can do that processing from my desktop application. 

    Do you know of any samples using RAPI/RAPI2 in vb.net to get me started?  I downloaded a bunch of test applications written in C# from codeplex and although these were excellent they did not demo file copying (and were in C#)?  VB samples would be wonderful.

    Thank you for your help


    With thanks, NECoder
    Thursday, March 03, 2011 7:24 AM
  • Thanks for all your help everyone.  Created a forms app in Visual Studio and added some RAPI code to achieve my end goal.  My problems was I was trying to run the code from the device - as Ilya pointed out above - RAPI is designed to run from desktop only.

    Solutiuon:

    create new vb.net forms app

    add RAPI2.dll reference (get RAPI2 from Codeplex.com)

    In form import System.devices namespace

    Here's a dump of my code for anyone who needs it (note -change file references to something meaningful to you) :-

     

    Imports

     

    System.Devices

    Public

     

    Class Form1

     

    Dim dev As RemoteDevice

     

    Dim mgr As New System.Devices.RemoteDeviceManager

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    dev = mgr.Devices.FirstConnectedDevice

     

    If dev Is Nothing Then

    MsgBox(

    "No device connected")

     

    Else

    MsgBox(

    "Connected to: " & dev.Name)

     

    End If

     

    End Sub

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    Dim RAPIfile As RemoteFile

    RAPIfile.CopyFileToDevice(dev,

    "C:\Users\Ian\Documents\test.txt", "\\my documents\test.txt", True)

    RAPIfile =

    Nothing

     

    End Sub

     

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

     

    Dim RAPIfile As RemoteFile

    RAPIfile.CopyFileFromDevice(dev,

    "\\program files\ie_mobile\client.sdf", "C:\Users\Ian\Documents\client.sdf", True)

    RAPIfile =

    Nothing

     

    End Sub

    End

     

    Class


    With thanks, NECoder
    • Marked as answer by NECoder Thursday, March 03, 2011 8:22 AM
    Thursday, March 03, 2011 8:21 AM
  • I use a bought product from Sapphire Solutions to ftp my files.  below is an example I use to send my database from a WM6 device to my iSeries IBM computer:

     

    Public Sub TransFile()

            Dim ftperror As Integer

            Dim ToFile As String

            Dim msg As String = vbNullString

            Dim mya() As String

            Dim getfiles() As String

            Dim endstr As Integer

            Dim TrnFile As String = "/ftptcomm/V300/" & DeviceID & "/mail/" & DeviceID & ".sdf"

            Dim SndFile As String = Data_Path & "\Datafiles\PreSale.sdf"

            Try

                showstatus("Attempting to send Data file")

                ftperror = sFTPOpen(Host_IpAddress, DEFAULT_FTP_PORT, "UserName", "PassWord", lPassive, lTimeOut, SapphireFTPLicence)

                ftperror = sFTPPut(SndFile, TrnFile, FTP_TRANSFER_BINARY, SapphireFTPLicence)

                If ftperror = 0 Then

                    showstatus("sending Data file")

                Else

                    MsgBox(" The program could not send " & SndFile & " to " & TrnFile & "!  The file is in use or missing.  Reset your device one time or exit the data entry program normally to ensure the file is not in use and try to resend.")

     

                End If

            Catch ex As Exception

                MsgBox(" The program could not send your PreSale Database!  The file is in use or missing.  Reset your device one time or exit the data entry program normally to ensure the file is not in use and try to resend.")

            End Try

            Try

                getfiles = System.IO.Directory.GetFiles(Data_Path & "\TransFiles")

                Dim gfile As String

                If getfiles.Length > 0 Then

                    For Each gfile In getfiles

                        mya = gfile.Split(Diag)

                        endstr = mya.Length - 1

                        ToFile = "/ftptcomm/V300/" & DeviceID & "/mail/" & mya(endstr).ToString

                        ftperror = sFTPPut(gfile, ToFile, FTP_TRANSFER_ASCII, SapphireFTPLicence)

                        If ftperror = 0 Then

                            showstatus("sending Data file")

                        Else

                            MsgBox(" The program could not send " & gfile & " to " & ToFile & "!  Try to resend.")

     

                        End If

                    Next

                End If

                MsgBox("The program sent your transmission files and database to /ftptcomm/V300/" & DeviceID & "/mail/.  Please Read this Message to the IT person helping you.")

     

            Catch ex As Exception

                MsgBox(" The program could not send your text files!  Try to resend.")

            End Try

        End Sub

     

    Thursday, March 03, 2011 8:48 PM
  • i have a few questions: what have u declared to be used as your deviceID, datapath?

      i also cant make use of this line of code sFTPOpen(Host_IpAddress, DEFAULT_FTP_PORT....

    Wednesday, April 06, 2011 9:10 PM
  • This is what I am trying to do, but all I need is how to detect the connection to WMDC programmatically(C#) on the device run application?

    Friday, August 19, 2011 2:58 PM
  •  

     

    i am okay for your answer ,

    i have given password to that SDF file  and i brought that sdf file to desktop application and again i reset to that SDF file into mobile application .

     

    but problem is  sdf file losses security(password) or again need to install new sdf file  in mobile application

     


    i am thinking alternative for this one

    that is i don't want to get the sdf file in to desktop application . just getting the table data into desktop application.


    is it possible?
    Wednesday, August 31, 2011 5:25 AM