B4J Code Snippet Data Folder

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:
B4X:
Sub GetDataFolder (AppName As String) As 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
   Else
     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:

ThRuST

Well-Known Member
Licensed User
Longtime User
It will return something like: C:\Users\H\AppData\Roaming\AppName on Windows and File.DirApp on other platforms.

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?
 

Tirs

Member
Licensed User
Longtime 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:
B4X:
Sub GetDataFolder (AppName As String) As 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
   Else
     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.

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:

Tirs

Member
Licensed User
Longtime 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.
 

Peter Simpson

Expert
Licensed User
Longtime User
I regret not checking first...

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.

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

Erel

B4X founder
Staff member
Licensed User
Longtime 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).
 

Peter Simpson

Expert
Licensed User
Longtime 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).

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

Thank you...
 
Top