Small Basic: The Settings File & Temporary Files

The Settings File

If you create a program that’s used by different people, you might want to let the user configure the program according to his or her own preferences. For example, a user might prefer to set the size of the graphics window, or change the background color of the window. One way to let the user change these settings is to save them in what’s called a program’s settings (also called properties, configuration, or initialization) file. A settings file saves special values for a program; your program can use the values stored in this file to initialize some of the variables it uses. For example, instead of setting the width of the GraphicsWindow to 400 pixels inside the program, we can read this value from the settings file. This way, the user can change it.

The settings file is just a plain text file. You can save any kind of text in it. There’s no standard format, but normally you’d save one setting per line. Each line might contain a string in the form <setting>=<value>

Although you can name the settings files anything you want, the File object provides a method that simplifies dealing with setting files. The GetSettingsFilePath() method returns the full path of the settings file for a program. If your program is C:\Temp\MyApp.exe, this method returns C:\Temp\MyApp.settings.

Temporary Files

Some programs require temporary files during their execution. The files are temporary because they aren’t usually needed after the program ends. The program uses these files to store temporary results. For example, when you create a Small Basic program in the IDE and run it before you save it, the Small Basic compiler creates a temporary executable file (and other temporary files) in order to run your program. Once finished, it might delete these files because they’re no longer needed.

The File object provides the GetTemporaryFilePath() method to create a new temporary file for you; it returns the full file path of the created file. For example, look at the following statements:

path = File.GetTemporaryFilePath()
 
TextWindow.WriteLine(path)

That code displayed the following output in the text window:

C:\Users\Ed\AppData\Local\Temp\tmp6093.tmp

When I checked the displayed path (of course, your path will be different), I found an empty file tmp6093.tmp there. Each time you call this function, a new temporary file is created for you.

Learn More

Do you have any questions? Ask us! We’re full of answers and other fine things!

Head to the Small Basic forum to get the most answers to your questions: 

http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads/   

And go to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!

See Also