B4J Question List available drive names

ThRuST

Well-Known Member
Licensed User
Longtime User
I need a way to list available drive names, preferably with Java v9.0.4

example
C:\ Maindrive_C
D:\ Studio
E:\ My Appz

I found this code, which only lists the available drives on PC, but not their names. How to list names also?
I need a solution only for PC in this case. Is there a way to get the drive names from the example below?

B4X:
For Each drive As String In ListRoots
   Log(drive)
Next

Sub ListRoots As List
   Dim jo As JavaObject
   Dim o() As Object = jo.InitializeStatic("java.io.File").RunMethod("listRoots", Null)
   Return o
End Sub
 
Last edited:

Daestrum

Well-Known Member
Licensed User
Longtime User
Change your for each to
B4X:
 Dim fsv As JavaObject
 fsv.InitializeStatic("javax.swing.filechooser.FileSystemView")
 For Each drive As Object In ListRoots
  Log(drive)
  Log(fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemDisplayName",Array(drive)))
 Next

It outputs like this

DATA (D: )
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Daestrum Nice solution. I put it in a source code. Feel free to update it as you please. It would be nice to add more info as well, so if you know how to do it throw it in there as well in the example. Drive types would be nice and perhaps available space too. Cheers
 

Attachments

  • Drive Names.zip
    15.9 KB · Views: 154
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@inakigarm Great addition indeed. I have a follow up question to Daestrums post. I tried to get the lenght of the drive name so I can use substring and substract the lenght by -3 to remove the "C:\" etc at the end, since I am adding it like this

C:\ MainDisk
D:\ Studio

So how can I get the complete drive name and the lenght at the same time. The problem is that it is an object, and that's why the Substring method does not work.
Hopefully the object can be converted to string in the same loop to get the lenght of the drivename.
 
Upvote 0

Daestrum

Well-Known Member
Licensed User
Longtime User
B4X:
  Dim dr As String = fsv.RunMethodJO("getFileSystemView",Null).RunMethod("getSystemDisplayName",Array(drive))
  Log("Display Name : "&dr.SubString2(dr.LastIndexOf("(")+1,dr.Length-1)&"\ "&dr.SubString2(0,dr.LastIndexOf("(")-1))
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Daestrum Nice. However I solved it another way since I am using the FloatingMenu Class by Steve05.

This is how it's done :)

FloatingMenu Class
B4X:
'Create and return a TextMenuItem Object #2
Public Sub MenuText2(Text As String) As FMItemTextClass
    Dim MI As FMItemTextClass
    
    If Text.Length >= 8 Then
        MI.Initialize(Me,"MI",Text.SubString2(0,Text.Length-4))
        
        Else
            MI.Initialize(Me,"MI",Text & " [CD-Rom/DVD/Virtual Drive]")

    End If
    'Add a style to the MenuItem
    FMUtils.AsJO(MI.ASJavaObject).RunMethodJO("getStyleClass",Null).RunMethod("addAll",Array(Array As String("cmenu","menutext")))
    Return MI
End Sub

This is the result. Thank you master @Daestrum

upload_2018-12-27_21-6-39.png
 
Upvote 0
Top