views:

122

answers:

3

To jog everyone's memory, Java has these files with an extension of ".properties", which are basically an ASCII text file full of key-value pairs. The framework has some really easy ways to suck that file into (essentially) a fancy hashmap.

The two big advantages (as I see it) being extreme ease of both hand-editing and reading/writing.

Does .Net have an equivalent baked in? Sure, I could do the same with an XML file, but I'd rather not have to hand type all those angle brackets, if you know what I mean. Also, a way to suck all the data into a data structure in memory in one line is nice too.

(Sidebar: I kind of can't believe this hasn't been asked here already, but I couldn't find such a question.)

Edit:

To answer the question implied by some of the comments, I'm not looking for a way to specifically read java .properties files under .net, I'm looking for the functional equivalent in the .net universe. (And I was hoping that it wouldn't be XML-based, having apparently forgotten that this is .net we're talking about.)

And, while config files are close, I need way to store some arbitrary strings, not app config information, so the focus and design of config files seemed off-base.

+5  A: 

The .NET way is to use a configuration file. The .NET framework even offers an API for working with them.

Andrew Hare
Aren't config files meant for just application config data? I really just want an easy way to pull some strings out of a text file. I don't want the framework to think I'm trying to configure something. :)
Electrons_Ahoy
+1  A: 

You can achieve a similar piece of functionality to properties files using the built in settings files (in VS, add a new "Settings file") - but it is still XML based.

You can access the settings using the auto-generated Settings class, and even update them and save them back to the config file - all without writing any of the boilerplate code. The settings are strongly-types, and can be specified as "User" (saved to the user's Application Data folder) or "Application" (saved to the same folder as the running exe).

adrianbanks
Aha! That was exactly what I was looking for. Thanks!
Electrons_Ahoy
+1  A: 

There is a third party component called nini which can be found at sourceforge.net

For an example:

using Nini;
using Nini.Config;

namespace niniDemo{
   public class niniDemoClass{
       public bool LoadIni(){
            string configFileName = "demo.ini";
            IniConfigSource configSource = new IniConfigSource(configFileName);

            IConfig demoConfigSection = configSource.Configs["Demo"];
            string demoVal = demoConfigSection.Get("demoVal", string.Empty);
       }
   }

}

The above sample's 'demo.ini' file would be:

[Demo]
demoVal = foobar

If the value of demoVal is null or empty, it defaults to string.Empty.

Hope this helps, Best regards, Tom.

tommieb75
Oooh, neat. Good to know! Thanks!
Electrons_Ahoy