Serialization vs Binary File
-
I made a simple configuration class to store program settings. My reason for this was to make the form application more portable (no HDD installation required), able to run on removable drives (e.g., USB), and did not write information to the user's profile or the registry. The answer was obviously a binary data file. My first test was serialize/deserialize my properties using BinaryFormatter in a serializable class. Although I could have used XML serialization, I chose the binary file route versus text. The test second option was to simply read and write the properties using BinaryReader and BinaryWriter. The resulting output file (I call settings.bin) is about 1,000 bytes larger with the serialization technique then with the BinaryWriter option. My question is; are there any performance differences between the two? Thanks, Mark
-
I made a simple configuration class to store program settings. My reason for this was to make the form application more portable (no HDD installation required), able to run on removable drives (e.g., USB), and did not write information to the user's profile or the registry. The answer was obviously a binary data file. My first test was serialize/deserialize my properties using BinaryFormatter in a serializable class. Although I could have used XML serialization, I chose the binary file route versus text. The test second option was to simply read and write the properties using BinaryReader and BinaryWriter. The resulting output file (I call settings.bin) is about 1,000 bytes larger with the serialization technique then with the BinaryWriter option. My question is; are there any performance differences between the two? Thanks, Mark
Mark, BinaryReader/Writer should be much faster, however as you are using it only on a small settings file that will propably only be read at startup and saved only when the user changes something. The BinaryFormatter should be fine. However writing your own file format is somewhat additional work, it simplify upgrades of your software. The formatter might fail when you add, change or remove fields in your settings. Why can't you use System.Configuration do the work for you, it was made to handle settings (stored in .config files so you can put them on your USB stick). Regards, Jelle Hissink
-
Mark, BinaryReader/Writer should be much faster, however as you are using it only on a small settings file that will propably only be read at startup and saved only when the user changes something. The BinaryFormatter should be fine. However writing your own file format is somewhat additional work, it simplify upgrades of your software. The formatter might fail when you add, change or remove fields in your settings. Why can't you use System.Configuration do the work for you, it was made to handle settings (stored in .config files so you can put them on your USB stick). Regards, Jelle Hissink
Jelle Hissink wrote:
Why can't you use System.Configuration do the work for you, it was made to handle settings (stored in .config files so you can put them on your USB stick).
Because the settings files are written to the user's "%UserName%\Application Data\Program_Name\" folder, and these are also XML (text) files. The binary disk file, is in the same folder as the application and is always available. So, if I move the application to another PC my settings are there too. This obviously wouldn't work with registered DLL's and other support files that would require a hard disk install. Mark
-
Jelle Hissink wrote:
Why can't you use System.Configuration do the work for you, it was made to handle settings (stored in .config files so you can put them on your USB stick).
Because the settings files are written to the user's "%UserName%\Application Data\Program_Name\" folder, and these are also XML (text) files. The binary disk file, is in the same folder as the application and is always available. So, if I move the application to another PC my settings are there too. This obviously wouldn't work with registered DLL's and other support files that would require a hard disk install. Mark
You can extend the SettingsManager in .Net (search for the SettingsProvider class). It allows you to implement your own read/save mechanism for settings. Some links: Google Some random results from google: VB.Net example saving all settings in seperate files C# example writing to .ini file a forum thread trying to implement a SettingsProvider Jelle