B4J Code Snippet Data Folder

Discussion in 'B4J Code Snippets' started by Erel, Aug 3, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Edit: This method is now available internally with File.DirData.

    Windows apps are installed by default under Program Files. Starting from Windows Vista, Program Files folders are read-only. This means that writing to File.DirApp will fail.

    The following code returns a writable folder based on the platform:
    Sub GetDataFolder (AppName As StringAs String
    Dim os As String = GetSystemProperty("os.name""").ToLowerCase
    If os.Contains("win"Then
    Dim wf As String = File.Combine(GetEnvironmentVariable("AppData"""), AppName)
    File.MakeDir(wf, "")
    Return wf
    Return File.DirApp
    End If
    End Sub
    It will return something like: C:\Users\H\AppData\Roaming\AppName on Windows and File.DirApp on other platforms.
    Last edited: Jan 3, 2016
    Kiffi, Tayfur, DonManfred and 14 others like this.
  2. ThRuST

    ThRuST Well-Known Member Licensed User

    Does File.DirApp contains the project folder on non-Windows systems? Because it is not created in the above routine. So where is File.DirApp on Mac etc?
  3. Erel

    Erel Administrator Staff Member Licensed User

    This thread has nothing to do with File.DirApp.
    Please start a new thread for your question.
  4. Tirs

    Tirs Member Licensed User

    Well, File.DirData breaks the program when run under Windows XP, so this small piece of code is still useful. Thank you, Erel!
    Of course, I understand XP is not supported any more, so I don't consider this to be a bug.
    (Yes, I'm still building apps for Windows XP computers. Don't ask. Fortunately, they are not connected to the Internet).
    Last edited: Feb 9, 2018
    jahswani likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    If I remember correctly File.DirData implementation is identical to the above code. What happens when you call File.DirData on Windows Xp?
  6. Tirs

    Tirs Member Licensed User

    Well, that's really odd...
    I removed the call to GetDataFolder and put back the File.DirData ...and now it works. I cannot reproduce the error any more. The program logic hasn't changed, so it's quite strange.
    I was getting a Java "File not found" exception. But it doesn't happen any more: now it works properly.
    Sorry I'm unable to give more info.
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    I regret not checking first...

    I had no idea that it was this easy (well for you to figure out Erel) to get the Roaming folder location using B4J. If I had realised I could have gotten to the roaming folder this easily, I would have developed B4A TM using B4J, I really had absolutely no idea, but then again I didn't really look.

    You have to be kidding me.
    I just quickly checked and found the following code too (less than 2 minutes of searching the forum) https://www.b4x.com/android/forum/t...y-on-mac-linux-and-windows.61655/#post-389087

    Admittedly I only had a day to write B4X TM and learning what I needed to learn to develop it using B4J would have taken me half a day in itself (learning only). When I get couple of days free, I will definitely rewrite B4X TM using B4J as the rest of the program is just about utilising some basic views.

    You live and learn something new every single day, thank you Erel...
    DonManfred and Erel like this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    Cross platform tip:

    XUI.DefaultFolder returns a "good" default folder for reading and writing. It is File.DirInternal in B4A, File.DirDocuments in B4i and File.DirData in B4J. You need to call XUI.SetDataFolder once to set the the data subfolder name (it doesn't do anything in B4A and B4i).
  9. Peter Simpson

    Peter Simpson Expert Licensed User

    Excellent tip Erel,
    XUI is turning out to have more features and is more powerful than I first thought it had.

    Thank you...
  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