1. Black Friday Sale - 50% off

    B4A Standard for $29 instead of $59!
    Buy Now
    Dismiss Notice

B4J Code Snippet Find user documents folder

Discussion in 'B4J Code Snippets' started by Erel, Jan 3, 2019.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub FindUserDocumentsFolder As String
       
    If DetectOS = "windows" Then
           
    Dim jo As JavaObject
           
    Dim fs As JavaObject = jo.InitializeStatic("javax.swing.filechooser.FileSystemView").RunMethod("getFileSystemView"Null)
           
    Return fs.RunMethodJO("getDefaultDirectory"Null).RunMethod("getPath"Null)
       
    Else
           
    Return GetSystemProperty("user.home""") & "/Documents"
       
    End If
    End Sub

    Sub DetectOS As String
       
    Dim os As String = GetSystemProperty("os.name""").ToLowerCase
       
    If os.Contains("win"Then
           
    Return "windows"
       
    Else If os.Contains("mac"Then
           
    Return "mac"
       
    Else
           
    Return "linux"
       
    End If
    End Sub
    Tested on Windows and Mac only.
     
    Segga, ThRuST, jimmyF and 4 others like this.
  2. moore_it

    moore_it Active Member Licensed User

    WOW thanks very much !
     
  3. ThRuST

    ThRuST Well-Known Member Licensed User

    What is the prefferable way to open the path in explorer/finder

    ShowExternalDocument or Shell?
     
  4. DonManfred

    DonManfred Expert Licensed User

    Code:
    Sub Process_Globals
        
    Private shl As Shell
    End Sub

    Sub shl.Initialize("Shell","cmd",Array As String("/c""start""E:/Basic4android/Projekte/libs.DonManfred/"))
        shl.Run(
    5000)
    End Sub
    Sub Shell_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        
    Log($"Shell_ProcessCompleted(${Success}, ${ExitCode}, ${StdOut}, ${StdErr})"$)
    End Sub
    It opens this Folder in my Explorer. Just tested on Windows

    [​IMG]

    Using ShowExternalDocument it works this way

    Code:
    fx.ShowExternalDocument(File.GetUri("E:\Basic4android\Projekte\libs.DonManfred""CalendarView"))
    It opens E:\Basic4android\Projekte\libs.DonManfred\CalendarView\


    It does work this way too
    Code:
    fx.ShowExternalDocument(File.GetUri("E:\Basic4android\Projekte\libs.DonManfred"""))
    It opens E:\Basic4android\Projekte\libs.DonManfred

    Based on these two examples i would prefer the ShowExternalDocument Solution as you do not need to start a Shell and a cmd instance.

    PS: I cannot say anywhing about Mac; you need to test it.
     
    Last edited: Jan 5, 2019
    ThRuST likes this.
  5. DonManfred

    DonManfred Expert Licensed User

    If you want to Open the Explorer and select a file/Folder

    Code:
    Sub Process_Globals
        
    Private fx As JFX
        
    Private MainForm As Form
        
    'Private shl As Shell
        Private NativeMe As JavaObject

    End Sub
    Code:
    Sub AppStart (Form1 As Form, Args() As String)
        NativeMe = Me
        MainForm = Form1
        MainForm.Show
        NativeMe.RunMethod(
    "OpenExplorer"Array As String(File.Combine("E:\Basic4android\Projekte\libs.DonManfred","CalendarView")))
    End Sub
    #If java
    import java.lang.Runtime;
    public static void OpenExplorer(String path){
       try {
           Runtime.getRuntime().exec("explorer.exe /select," + path);
       } catch (Exception e) {
       
    }
    }
    #End If
    It opens the Folder E:\Basic4android\Projekte\libs.DonManfred and select the Folder CalendarView

    [​IMG]
     
    inakigarm and ThRuST like this.
  6. ThRuST

    ThRuST Well-Known Member Licensed User

    @DonManfred Very nice. This is most useful, I will try this on my virtual Mac which is running OSX El Capitan.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice