Save a simple INI-file


i am a Xojo-starter and i would like to save some simple parameters from my program. But after one day of googeling it seems that it is not possible to load that parameters at the start of the program and to save it at the end without dialog. In a German forum i got an example with constructors, pref datatypes, operator lookups an so on…

I only want to save some parameters. (It is so easy in VB6.0). :frowning:

Have a look at . Has links to several examples.

Yes, i worked for hours in that discussion. I found it in the German discussion. But its much to complicated…
The most of them prefer SQLite. (i can’t believe that!)

I only want to save some signs in a .txt file ;-(

The INI-Class I wrote mentioned in that above topic is used in every project I write and works perfectly cross platform and is clear text easily read/written by a standard text editor if required.

That is what i wrote. I know that all. It is complete to much and to difficult for me. My “project” has the half size of that example. I have to train for some days only to understand the lookups, constructors and so on. I can’t use special self-created classes. I will not understand that.

That is my problem with Xojo. Everybody can do everything. But it seems that the saving of some signs are very difficult.

Ok… If you find my ini file too difficult to use, then how did you ever understand using the VB6 version?
You drop my code in… and there are 3 subs to controls it. One to set defaults, one to load the data, one to save the data…
What is INSIDE the class is not really important if you just want it to work…

But hey, that is up to you. You have asked the question, recieved answers and decided to reject the help provided… what more do you expect us to do for you?

Hmmm, i only need an idea to do in Xojo things like that: (VB6.0)

Datei = FreeFile Open App.Path & "\\Ini.txt" For Input As #Datei Input #Datei, Parameter1 Input #Datei, Parameter2 Input #Datei, Parameter3 Close #Datei
That is all i need!
Maybe your code is brilliant, but i do not understand that ;-(

What have you tried? How has it failed?
Have you read the documentation?

I am at this time based on your previous posts assuming the answers are
Nothing, I don’t know, and No

I tried so much. In the most of time nothing happened like this:

dim FI as FolderItem dim TOS as textOutputStream //****************** FI = new FolderItem FI = FI.Child ("Connect.txt") TOS = TextOutputStream.Create(FI) TOS.WriteLine ("HALLO") TOS.Close

That works perfect but only with the dialog: ;-(

Dim t As TextOutputStream Dim f as FolderItem f=GetSaveFolderItem("Test.txt","") If f <> Nil Then t = TextOutputStream.Create(f) t.Write("Hello!") t.Close End If

The code works (the first one). Check for the file in your Xojo application folder (not you application folder).

 dim FI as FolderItem
  dim TOS as textOutputStream
  FI = new FolderItem
  FI = FI.Child ("Connect.txt")
  TOS = TextOutputStream.Create(FI)
  TOS.WriteLine ("HALLO")

this won’t work becuase FI is NIl
you do not use NEW with Folderitem

 dim FI as FolderItem
  dim TOS as textOutputStream
  FI =specialfolder.desktop.Child ("Connect.txt")
  if FI<>nil then 
  if fi.exists then fi.delete ' not required but here for illustration
  TOS = TextOutputStream.Create(FI)
  TOS.WriteLine ("HALLO")
  end if

this writes the file to your desktop… I am going to let you take these clues and figure out how to write it someplace else.

It should create - when debugging - a file in the Xojo application folder (at least it does on OS X).

create what? FI is NIL

NIL dot child is still NIL as there is no parent

FI = new FolderItem <— not required… but FI is still NIL
FI = FI.Child (“Connect.txt”) the child of NIL is still NIL

Hello Dave. Thank you for your code example. I tried the second one, but nothing happened. The file “Connect.txt” doesn’t exist.
What is wrong?

I specified that I was talking of the first of his two examples. Which works, but creates the file in the Xojo application folder, which is probably not wise.

dim FI as FolderItem dim TOS as textOutputStream //****************** FI = SpecialFolder.ApplicationData() FI = FI.Child ("Connect.txt") TOS = TextOutputStream.Create(FI) TOS.WriteLine ("HALLO") TOS.Close

This will put a file called “Connect.txt” in your user AppData directory (most likely in the Roaming profile)
To read it

dim FI as FolderItem dim TOS as textInputStream //****************** FI = SpecialFolder.ApplicationData() FI = FI.Child ("Connect.txt") TOS = TextinputStream.Open(FI) dim parameter as string = TOS.ReadLine TOS.Close

Things to know

  1. if another program has written a file called “Connect.txt” in the same location you may be reading your or theirs
    This is why it’s usually prudent to also put your data inside a directory with the name of your company or application as the dir name
  2. the code I give has NO error checking (its possible the file can’t be created or open or that it can’t be written to or read)

Wow! Norman that works! How can i manage the folder problem? My INI-file is in the project folder. So i do not need a specialfolder and his child?

Don’t put it there.
Windows will refuse to allow you to save files in the same folder as the program.
Same goes for Mac app store apps.

Always create a folder in ApplicationData with the name of your app
Then save and read your file from there

FI = SpecialFolder.ApplicationData.child("MyAppName") FI = FI.Child ("Connect.txt") TOS = TextOutputStream.Create(FI) TOS.WriteLine ("HALLO") TOS.Close

Remember that the first time your app starts there will be no file and no folder

So when the app starts, initialise your variables with sensible defaults
Then if the folder is missing , create it.
If the file is missing, create it using the sensible defaults
Open the file and read the values (you may choose not to do that if you just created it… thats your choice)

When the app closes, save the file

I’ve used INI files for years, but I read where others just used a simple SQLite database to hold preferences called Settings.db and put it in App support folder. What I liked about that approach is if a user makes a change to a preference, it is easy to save the single change right there instead of writing out the file again. I’ve seemed to have less trouble with the db approach.

Hey! You are great!. That is what i need! Some rows of text. That is what i understand.
If i am much more better i Xojo, i will put it in a SQlite.
Thanks to all!