Android Question Hi, I am saving a unique ID on local file, but file.exists is not working

omarruben

Active Member
Licensed User
Longtime User
B4X:
Sub GenerateGUID As String
 
    ' Get date and scramble it
    Dim d As Long = DateTime.Now
    Dim sd As String = ScrambleDate(d) 'Shuffle method scrambles a string
 
    ' Generate random hex string
    Dim hex As String = "0123456789abcdef"
    Dim r As StringBuilder
   
    r.Initialize
    Dim i As Int
    For i = 0 To 15
        r.Append(hex.CharAt(Rnd(0, 15)))
    Next
   
    Dim f As String = File.DirInternal & "ids.txt"

    If File.Exists(File.DirInternal,f) Then
        Log("file exists")
        'File already exists, read existing ID
        Return File.ReadString(File.DirInternal,f)
    Else
        'File doesn't exist, save new ID
        File.WriteString(File.DirInternal, "ids.txt", sd & r.ToString)
        Return sd & r.ToString
    End If
 
End Sub

the function File.Exists never finds the file so I get a new unique ID every single time a run the app
 

JohnC

Expert
Licensed User
Longtime User
Change the code as follows:
B4X:
Dim f As String = "ids.txt" 'remove File.DirInternal because you are already using that in the line "If File.Exists(File.DirInternal,f) Then"
 
Upvote 0
Top