Saving Registration Code

I have always had problems saving my product registration code in the correct location on Windows. I have a customer who says he has to register the program every time he launches which would indicate I am not saving to an accessable location. The customer is on Windows 11 (I only have access to Windows 10) he installs as an admin user for all accounts.

  • Is this the right location?
  • Is there a difference between W10 and W11 that I have to cater for, it looked to be the same location in the documentation.

I currently do the following which seemed to be working on Mac and Windows 10.

Dim tout as textoutputstream
Dim f as folderItem

f = SpecialFolder.Preferences.child("ProductRegoFileName")

if f <> Nil then
  'tout = f.CreateTextFile
  tout = TextOutputStream.Create(f)
  if tout <> nil then
    
    tout.writeline username
    tout.writeline company
    var ob as string = Bans
    dim encryptserial as string =EncryptQuickrc4v6(serialnum,ob)
    tout.WriteLine encryptserial
    
    'tout.writeline serialnum
    tout.writeline str(site)
    
    //tout.writeline settings
    tout.close
  end if
end if

Does this mean he logs in as “AdminUserXYZ” and then expects this to work for “RegularUserABC”? If so that is why it isn’t working. The actual location this would be stored to is \Users\AdminUserXYZ\AppData\Roaming\ so only AdminUserXYZ will see that file. If this needs to be available for multiple user logins then the location you probably want is SpecialFolder.SharedPreferences.

The locations will be identical for Windows 10 and 11.

I’d be using
SpecialFolder.SharedApplicationData.child(“ProductRegoFileName”)

Same place as SharedPreferences on Windows, but SharedPreferences may not be accessible for writing on Mac, I seem to recall

2 Likes

That makes sense. I’m guessing the network users in schools have been copying that file to all user preferences. Ok I’ll try that.

Looks like I better test both :slight_smile:

Thanks

When I change from Preferences.child to either of the suggestions the TextOutputStream returns Nil error?

What am I missing, It works just to the Preference Folder but as soon as you try either of the Shared Folders it doesn’t?

Using your folder name I do the following

Var DatabaseFolder As FolderItem
DatabaseFolder = SpecialFolder.SharedPreferences.Child("TestRego")
If Not DatabaseFolder.Exists Then
  DatabaseFolder.CreateFolder
End If

Then create your file after the folder has been created.

Still no luck

The code I gave you creates a new folder as C:\ProgramData\TestRego on my system. Perhaps it is a permissions issue on your system with the user you are running the code as? I always run logged in as a member of the Administrator group.

Not too sure on what is stopping it but I am in as an admin user and running on a Mac. It has worked fine for some time just writing to the Preference folder but the point you raised at the top of the discussion got me keen to get a solution for all users on the system not just the currently logged in one. I would have thought just changing to SpecialFolder.SharedPreferences would have been enough.

I’m running a Win 11 machine and have a Win 10 VM so unfortunately I can’t test what it does on macOS.

I’m guessing it is suppose to behave the same.

This thread is on Windows section, and:

So, are you using Windows, Mac or both?
If Mac, maybe newer macOS can’t write to SharedPreferences?

If you read:
https://documentation.xojo.com/api/files/specialfolder.html#specialfolder

it mentions Preferences folder for Windows but recommends SpecialFolder.ApplicationData to save your files.

Note: I don’t work much on Desktop apps, much less on Windows.

1 Like

Thanks Alberto, very helpful.

When you have such an exception, the first thing you may do to get a clue is to click on the exception (in the variables viewer) and look at its error code (and error message, if any). If you stop at “I get an exception”, you basically just know something went wrong, but it’s hard to guess what.

1 Like

Not only that, @Martin_Fitzgibbons , when you get this sort of error you should look through the properties of the folderitem f manually using the debugger and check that they all make sense - e.g., is f readable? is it writeable? Your code should probably check each of these.

Hi Arnaud,
The Error code was 13

Hi Tim,
f is no readable or writeable for SpecialFolder.SharedPreferences.Child(“TestRego”)

Hi Martin,
Ok, error 13 means several things, but in this context, the only relevant one would be “permissions denied”.

BTW: my comment was also meant to tell it’s a good habit to take in general: when getting an exception, check the error code, message and exception type right away.

1 Like