Can't create file in SpecialFolder.ApplicationData in Windows

My application saves user preferences in a file in SpecialFolder.ApplicationData. It works fine on Mac and Linux platforms but fails on Windows 7. (I don’t have a newer version of Windows for testing, because it’s not an OS that I use.)

'Save prefs
dim saveFail As Boolean = False
if windPrefs.ContentsChanged then
  dim f As FolderItem = SpecialFolder.ApplicationData.Child("MyAppFolder")
  if not f.Exists then
    f.CreateAsFolder
  end if
  dim ff As FolderItem = SpecialFolder.ApplicationData.Child("MyAppFolder").Child("MyAppPrefs.dat")
  if ff.Exists then
    Dim t As TextOutputStream
    t = TextOutputStream.Create(ff)
    if t=nil then
      saveFail=True
    Else
      dim prefString As String
      '
      'Code to load pref data into prefString      
      '
      t.Write(EncodeHex(prefString))
      t.Close
      MsgBox "Saved your preferences."
    end if
  Else
    saveFail=True
  end if
end if
if saveFail then MsgBox "Unable to save your preferences."+_
"They will revert to default values the next time the application runs."

The Windows version will read the MyAppPrefs.dat if it already exists, and update and it as necessary. But it won’t create it if it doesn’t already exist. The Mac and Linux versions of my app will create the file if it doesn’t exist.
On Windows it returns the “Unable to save your preferences. They will revert to default values the next time the application runs.” message

Any ideas of what I’m missing?

You should create the file if it doesn’t exist…your code creates it “if ff.Exists”.

2 Likes

try this:


if ff.Exists then
try
ff.delete
catch
//couldnt delete
end try
end if

    Dim t As TextOutputStream
    t = TextOutputStream.Create(ff)
    if t=nil then
      saveFail=True
    Else
      dim prefString As String
      '
      'Code to load pref data into prefString      
      '
      t.Write(EncodeHex(prefString))
      t.Close
      MsgBox "Saved your preferences."
    end if
  Else
    saveFail=True
  end if

So you have several ways of SaveFail becoming true. The first hint would be to know which one is the reason of the fail.
One way would be to change SaveFail to a string (or, at least, an integer, like an error code). You could have SaveFail=“” in the beginning, and set it to readable values when it fails (SaveFail=“t is nil”, SaveFail=“file doesn’t exist”) etc. and “if SaveFail<>“” then MsgBox “Unable to save your preferences (”+SaveFail+")…

Another thing I’m seeing is you don’t seem to handle exceptions. That’s not your current issue, but some functions may trigger exceptions with file handling. You’d be safer in anticipating for them.

Think algorithmically, then write the blocks of the desired algorithm.

CreatePrefsFolderIfItDoesNotExists()

If PrefsFileExists() Then
  LoadPreferences()
Else
  CreateDefaultPreferencesFile()
  LoadPreferences()
End

Once you see it, you may notice something that could be better as

CreatePrefsFolderIfItDoesNotExists()

If Not PrefsFileExists() Then CreateDefaultPreferencesFile()

LoadPreferencesFile()

Yes, that was it. Thank you.

I was sidetracked into thinking it was some odd Windows peculiarity, since it worked on both Mac and Linux installations. But I later realized that the Mac and Linux machines already had a copy of the prefs file from a previous installation.

I shouldn’t try to debug late at night when I’m tired. :roll_eyes:

1 Like

I’m glad the problem is solved…