I’m working on a little personal project to write a GUI editor for some specialised data files – It’s taking longer than I hoped which is why I’ve not Posted for a while. One hurdle is that I need to use a lot of reference data which is currently available in Java style “.properties” files. This is itself a specialised Java specific file format – popular in the Java world but, unsurprisingly, not supported by C#.
I had a quick hunt around the internet and came across a couple of links suggesting very simple ways to achieve something similar (such as this one).
Unfortunately, the “.properties” format is significantly more complex than this suggests and supports a broad range of syntax elements to handle comments and non-ascii characters – and to embed spaces and equals in the keys. Take heart, C# programmers though – one Java programmer had added the line “Hong Kong = Hong Kong” in a language properties file – not realising that the property name here is “Hong” and the property value is “Kong = Hong Kong”. The correct property should be written “Hong Kong = Hong Kong” (escape the first space).
So, to ensure I didn’t have any problems if the properties files I used were anything but basic, I thought I better implement something a bit more capable. The obvious solution is to implement a C# class based on the existing Java version – so this I have done, resulting in the
Kajabity.Tools.JavaProperties class (
Download Source ZIP (removed): JavaProperties.zip. The use of this software is entirely at your own risk.
This version uses C# friendly names and implements the most important features of the Java Properties class (as far as I can see) – including the Load and Store methods. It extends the System.Collections.Hashtable class and all of it’s features are available. I have improved slightly on the original and it should be more robust – for example when non-strings are added to the class I use the C# Object.ToString() to convert them to strings.
The zip also includes some sample “.properties” files to illustrate the features – and to show why a simpler parser won’t suffice.
Hope you find this useful.