Saving settings in the language selection form
Someone recently sent me an error message from the language selection form, which uses Isolated Storage to save its settings. The file in which the settings are stored appeared to be locked by another process.
I have no idea why the file was locked, but my instinct is to throw out the isolated storage completely.
This code is at least 10 years old and back then isolated storage was recommended for saving user settings. I always thought it was too complicated, but I wanted to do it in a .NET way in the brave new .NET world.
It is now very easy to save user settings, if you add them via the project properties in Visual Studio. This is how you could change the code:
The following example is for C# in a WPF project, but it is more or less the same in VB and probably identical for a Windows Forms application.
Add two settings
- LanguageStartupMode, as an Integer with the default value 2. This value means that the dialog will be shown at program startup.
(Do not use the default value of 0, because this means that the form will not appear, even the first time that the program is started.) - SelectedLanguage as a string.
Change the LoadSettings() function
This code is in the file SelectLanguage.cs or SelectLanguage.xaml.cs.
Replace this old code
| Old code | Copy to clipboard
|
| New Code | Copy to clipboard
|
Replace WpfApplication2 with the namespace of your application.
Change the SaveSettings() function
Replace this old code
| Old code | Copy to clipboard
|
| New Code | Copy to clipboard
|
Replace WpfApplication2 with the namespace of your application.
I think you will agree that this code is simpler than the original version using isolated storage.
To be honest, I don't know whether there could still be a file access problem reading the settings. If there is, then it would be a problem in Microsoft's code.
At present, I have not changed the template file. I might do this in a future version, but first I will have to figure out how to add the user settings automatically.😊
Phil