Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

Need help with my code to create folder on Win 6 Mobile device using OpenNETCF RAPI

Hi

This is code that I am working on that is supposed to check to see if a couple of folders exist on a Windows Mobile 6 Device. If they do not exist then these folders will be created. Unfortunately it does not work and throws an error (see code below) but the error message provides me with nothing to go on. I've tried multiple web searches and have come up empty.

Please help.

Thanks

PBLack

     
Imports OpenNETCF.Desktop.Communication
---------------------------------------------------------------
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       
            Dim strTallyDev As String = "\My Documents\Projects\Folder1"
            Dim strSetupdev As String = "\My Documents\Projects\Folder2"

            'check to see if project folders exists

            Dim directoryToListDev As String = "\My Documents\Projects\"
            'connect to device
            MyRapi = New RAPI
            MyRapi.Connect()

            Dim fileListDev As FileList = MyRapi.EnumFiles(directoryToListDev + "*"c)
            If fileListDev Is Nothing Then
                MyRapi.CreateDeviceDirectory(strTallyDev) 'Error thrown here - RAPIException was unhandled - Could not create directory
                MyRapi.CreateDeviceDirectory(strSetupdev)
            End If

            MyRapi.Disconnect()
            MyRapi.Dispose()
            MyRapi = Nothing
End Sub

Open in new window

0
PBLack
Asked:
PBLack
  • 3
  • 2
1 Solution
 
hjgodeCommented:
The problem is that you can not create a full path with multiple new dirs at once.

The other problem is that OpenNetCF.Desktop.Communication hides all the error details. Here is my addition to there code:
		public void CreateDeviceDirectory(string PathName)
		{
			CheckConnection();
			if(! Convert.ToBoolean(CeCreateDirectory(PathName, 0)))
			{
                int err = CeRapiGetError();
                int err2 = CeGetLastError();
                throw new RAPIException("Could not create directory: '" + PathName + "' RapiErr=" + err.ToString() + " ceErr=" + err2.ToString());
			}
		}

Open in new window


Now we see you will get CE error code 3L: ERROR_PATH_NOT_FOUND. And this lead me to the solution to split the path to the directory to create and use CreateDirectory on every part: A new function:

    Function makeFolder(ByVal p As String) As Boolean
        Dim result As Boolean = False
        Dim list As String()
        list = p.Split(New Char() {"\"c})

        Dim fullPath As String = "\"
        For ix As Integer = 0 To list.Length - 1
            If list(ix) <> "" Then
                fullPath += list(ix) + "\"
                Try
                    MyRapi.CreateDeviceDirectory(fullPath) 'error 187 already exists
                    result = True
                Catch ex As Exception
                    System.Diagnostics.Debug.WriteLine("exception in makePath: " + ex.Message)
                End Try
            End If
        Next

        makeFolder = result
    End Function

Open in new window


Here is your code changed using the new function:
Imports OpenNETCF.Desktop.Communication

Public Class Form1

    Dim MyRapi As OpenNETCF.Desktop.Communication.RAPI

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strTallyDev As String = "\My Documents\Projects\Folder1"
        Dim strSetupdev As String = "\My Documents\Projects\Folder2"

        'check to see if project folders exists

        Dim directoryToListDev As String = "\My Documents\Projects\"
        'connect to device
        MyRapi = New RAPI
        MyRapi.Connect()

        Dim fileListDev As FileList = MyRapi.EnumFiles(directoryToListDev + "*"c)
        If fileListDev Is Nothing Then
            Dim bRes As Boolean
            bRes = makeFolder(strTallyDev)
            'MyRapi.CreateDeviceDirectory("\My Documents") 'error 187 already exists                
            'MyRapi.CreateDeviceDirectory("\My Documents\Projects") 'error 03 = ERROR_PATH_NOT_FOUND
            bRes = makeFolder(strSetupdev)
            'MyRapi.CreateDeviceDirectory(strTallyDev) 'Error thrown here - RAPIException was unhandled - Could not create directory
            'MyRapi.CreateDeviceDirectory(strSetupdev)
        End If

        MyRapi.Disconnect()
        MyRapi.Dispose()
        MyRapi = Nothing
    End Sub
    Function makeFolder(ByVal p As String) As Boolean
        Dim result As Boolean = False
        Dim list As String()
        list = p.Split(New Char() {"\"c})

        Dim fullPath As String = "\"
        For ix As Integer = 0 To list.Length - 1
            If list(ix) <> "" Then
                fullPath += list(ix) + "\"
                Try
                    MyRapi.CreateDeviceDirectory(fullPath) 'error 187 already exists
                    result = True
                Catch ex As Exception
                    System.Diagnostics.Debug.WriteLine("exception in malePath: " + ex.Message)
                End Try
            End If
        Next

        makeFolder = result
    End Function
End Class

Open in new window

0
 
PBLackAuthor Commented:
I discovered this last night and was just coming here to close the question and see that you found it too so full points. I was not aware that with RAPI that the DeviceFileExists argument applied to folders too. When I discovered that It was a breeze to code.

For example I used this:
                 If Not MyRapi.DeviceFileExists("\My Documents\Projects" Then
                    MyRapi.CreateDeviceDirectory("\My Documents\Projects\" & item.Text)
                End If

                If Not MyRapi.DeviceFileExists("\My Documents\Projects\" & item.Text) Then
                    MyRapi.CreateDeviceDirectory("\My Documents\Projects\" & item.Text)
                End If

Thank you for your effort.
0
 
PBLackAuthor Commented:
oops small typo now fixed:

For example I used this:
                 If Not MyRapi.DeviceFileExists("\My Documents\Projects" Then
                    MyRapi.CreateDeviceDirectory("\My Documents\Projects")
                End If

                If Not MyRapi.DeviceFileExists("\My Documents\Projects\" & item.Text) Then
                    MyRapi.CreateDeviceDirectory("\My Documents\Projects\" & item.Text)
                End If
0
 
hjgodeCommented:
Hopefully you will not try to execute that on a device with german or french OS language pack.

Thanks

Josef
0
 
PBLackAuthor Commented:
It is for English Only but good point. I never thought of that.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now