Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private DriveLetterMap As Map ' contains a map of the network locations mapped to a drive letter
Type returnType( code As Int, msg As String )
Private fc As FileChooser
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
DriveLetterMap.Initialize
fc.Initialize
PopulateDriveLetterMap
End Sub
Sub Button1_Click
Private filename As String
fc.InitialDirectory = "C:\"
fc.InitialFileName = ""
Dim filename As String = fc.ShowOpen( MainForm )
Log( "Windows filename: " & filename )
Log( "URI filename: " & GetFilenameAsURI( filename ))
End Sub
Sub GetFilenameAsURI( filename As String ) As String
If filename.Length > 0 Then
Dim rc As returnType = replaceDriveLetterWithURI( filename )
If rc.code > -1 Then
filename = rc.msg
Else
Log( "Drive letter not found" )
End If
End If
Return filename
End Sub ' GetFilenameAsURI()
Sub PopulateDriveLetterMap
Wait For (NetUse) Complete (StdOut As String)
Dim r() As String = Regex.Split( CRLF, StdOut )
For Each s As String In r
If s.IndexOf(":") > -1 Then
Log( s )
Dim driveLetter As String = s.SubString2( s.IndexOf(":")-1, s.IndexOf(":")+1 )
DriveLetterMap.Put( driveLetter, s.SubString( s.IndexOf(":")+1 ).Trim )
End If
Next
End Sub ' PopulateDriveLetterMap
Private Sub NetUse As ResumableSub
Dim s As Shell ' requires the jShell library
s.Initialize("s", "net", Array("use"))
s.Run(60000)
Wait For (s) s_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
Return StdOut
End Sub ' NetUse
' replace drive letter with URI by collecting the drive letter mapping at runtime
Sub replaceDriveLetterWithURI( path As String ) As returnType
Dim e As returnType
e.Initialize
' set defaults if drive letter not found
e.code = 0 ' path does not contain a drive letter
e.msg = path ' return path as-is
If path.IndexOf( ":" ) > -1 Then
Dim driveLetter As String = path.SubString2( path.IndexOf(":")-1, path.IndexOf(":")+1 )
If driveLetter <> "C:" Then
If DriveLetterMap.ContainsKey( driveLetter ) Then
e.code = 1 ' path contained a drive letter
e.msg = DriveLetterMap.get( driveLetter ) & path.SubString( 2 )
Else If File.Exists( path.SubString2( 0, path.LastIndexOf( "\" )), path.SubString( path.LastIndexOf( "\" )+1) ) = False Then
' path includes a drive letter that is not C and not a mapped network drive and not secondary local storage
e.code = -1 ' path contained a drive letter that is not mapped to a network resource on this machine
e.msg = "Drive letter " & driveLetter & " not found on this machine"
End If
End If
End If
Return e
End Sub ' replaceDriveLetterWithURI()